嗯,一个答案原来只是使用requests package,并且每次都复制并粘贴重定向的 URL:
from requests_oauthlib import OAuth2Session
scope = 'all'
redirect_uri='http://localhost:8888/'
oauth = OAuth2Session('YourApplicationApiIdNumber', redirect_uri=redirect_uri, scope=scope)
authorization_url, state = oauth.authorization_url(
"https://api.mendeley.com/oauth/authorize" )
print( 'Please go to %s to authorize access, and copy the final localhost URL' % authorization_url )
assert(False) # Stop processing until this is done.
...变成一个变量:
authorization_response = 'http://localhost:8888/tree?TheStuffWereInterestedIn'
...然后从那里拿走:
token = oauth.fetch_token(
'https://api.mendeley.com/oauth/token',
authorization_response=authorization_response.replace('http', 'https').replace(',',''),
client_secret='YourClientSecret')
r = oauth.get('https://api.mendeley.com/documents?sort=last_modified&order=desc&limit=500', timeout=30)
您必须使用回调 URL 配置 Mendeley 应用程序界面。这是 http://localhost:8888/ ,因为这是 Jupyter 可以在不丢失其他 OAuth2 参数的情况下显示的内容。但是 Requests OAuth 实现不接受非 https 链接(也不接受 Jupyter 偶尔添加的尾随逗号),因此我们如图所示对其进行了篡改。
我猜这种方法几乎适用于任何 OAuth2 API。当然请求列表quite a few。