繁体   English   中英

如何在 python 脚本中使用 request.post 在没有密码的情况下向 bitbucket api 进行身份验证?

[英]how do you authenticate to bitbucket api without password in python script using request.post?

如何在 python 脚本中使用 request.post 在没有密码的情况下向 bitbucket api 进行身份验证?

我目前正在使用下面的这种方法, ('username', 'mypwd')但我想要另一种方法,

r = requests.post(url, auth=('username', 'mypwd'), headers=headers, data=json.dumps(my_data), timeout=30)

As you can read in this documentation of the Bitbucket REST API, it accepts basic auth, which is the one you're using, OAuth and Session Authentication. 所有这些都可以由请求模块处理。

只要我理解您的问题,您不希望您的密码出现在代码中,因此基本身份验证和 session 身份验证会在第一时间被丢弃(我们将在下面看到替代方案)。 OAuth 似乎是通往 go 的唯一途径,但您需要在每篇文章中发送或使用 App Keys 和 Token Secrets 等信息进行身份验证。

Another good way to go is to store the password in another file, and by using the os module and the function os.environ['VariableName'] you can extract it to a local variable on code and pass it to the requests function.

最后但同样重要的是,您可以使用getpass模块调用函数getpassgetuser ,这将提示您输入密码和用户名,并将它们保存到变量中。

注意:通过创建 requests 模块的session object ,您只需“登录”一次,之后没有其他 post/get 将需要身份验证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM