出于测试自动化的目的,我想使用requests和requestoauthlib库来访问API。你知道吗
API使用oauth2身份验证和google帐户,但是它不是googleapi。由于我的测试工具应该能够在无人值守的情况下运行,我希望能够获得一个访问令牌,然后我可以无限期地自动刷新它。你知道吗
从requests auth文档中看到这样的例子会很好。它需要手动登录一次,然后我可以刷新令牌。
https://requests-oauthlib.readthedocs.io/en/latest/examples/google.html
from requests_oauthlib import OAuth2Session
client_id="xxxx.apps.googleusercontent.com"
client_secret="xxxxx"
redirect_uri = 'https://localhost/callback'
authorization_base_url = "https://accounts.google.com/o/oauth2/auth"
token_url ="https://accounts.google.com/o/oauth2/token"
scope = [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
]
google = OAuth2Session(client_id, scope=scope, redirect_uri=redirect_uri)
# Redirect user to Google for authorization
authorization_url, state = google.authorization_url(authorization_base_url,
access_type="offline", prompt="select_account")
print 'Please go here and authorize,', authorization_url
redirect_response = raw_input('Paste the full redirect URL here:')
# Fetch the access token
google.fetch_token(token_url, client_secret=client_secret,
authorization_response=redirect_response)
r = google.get('https://www.googleapis.com/oauth2/v1/userinfo')
print r.content
但是,我需要适应我的API,我找不到方法。你知道吗
我可以设置授权\u base \u url=“https://example.net/.auth/login/google” 并获取重定向URL,但之后就不起作用了。 我也不知道应该设置什么范围。你知道吗
我应该从API提供者那里得到Id和secret吗? 还是有别的解决办法?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐