在python中进行后台OAuth2令牌通信



我正在尝试构建一个最基本的python应用程序,该应用程序使用OAuth2登录GitHub并从中获取我的用户名-使用请求oauthlib(这个:https://requests-oauthlib.readthedocs.io/en/latest/examples/github.html)。

这是那里的代码:

from requests_oauthlib import OAuth2Session
from flask.json import jsonify
client_id = <my GitHub id>
client_secret = <my GitHub secret>
authorization_base_url = 'https://github.com/login/oauth/authorize'
token_url = 'https://github.com/login/oauth/access_token'
github = OAuth2Session(client_id)
authorization_url, state = github.authorization_url(authorization_base_url)
print('Please go here and authorize,', authorization_url)
redirect_response = input('Paste the full redirect URL here:')
github.fetch_token(token_url, client_secret=client_secret,
authorization_response=redirect_response)
r = github.get('https://api.github.com/user')
print(r.content)

如果我将生成的链接添加到浏览器中,按enter键,url将像符咒一样变为localhost:8080(我在GitHub中提供的回调url(,并带有代码和状态参数。如果我在python代码中输入它,我就可以获取GitHub数据。我的问题是,有什么方法可以实现自动化吗?比如,跳过用户交互,只需在GitHub中询问凭据,然后在控制台中打印我的数据?感谢

您正在使用web应用程序流。出于您的目的,最好使用设备流。

基本上,用户需要添加您将在GitHub页面输入中提供的代码。由于您将其作为一个简单的应用程序使用,因此您可以在较低级别中这样做,并将简单的GET和POSTS请求与请求一起使用

最新更新