mendeley Python-SDK中的认证问题

2024-04-29 21:28:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我在读孟德尔的文档。我正在尝试在我的控制台中获取数据,我正在使用教程中的以下代码

from mendeley import Mendeley

# These values should match the ones supplied when registering your application.
mendeley = Mendeley(client_id, redirect_uri=redirect_uri)

auth = mendeley.start_implicit_grant_flow()

# The user needs to visit this URL, and log in to Mendeley.
login_url = auth.get_login_url()

# After logging in, the user will be redirected to a URL, auth_response.
session = auth.authenticate(auth_response)

现在我不明白最后一行代码中auth_response将从何而来?有人知道吗?谢谢


Tags: theto代码in文档authurlresponse
2条回答

据我所知,auth_response是由用户/服务器提供的。例如,我在github上找到了一个不错的代码块(完整源代码:link):

@app.route('/oauth')
def auth_return():
    auth = mendeley.start_authorization_code_flow(state=session['state'])
    mendeley_session = auth.authenticate(request.url)

    session.clear()
    session['token'] = mendeley_session.token

    return redirect('/listDocuments')

如您所见,作者似乎正在将客户端重定向回原来的位置请求.url. 在

我可以用下面的代码进行实验并使其工作,完全自动化。无需用户干预

client_id = 1
client_secret = "XXXXXXXXX"

redirect_uri = "http://localhost:8080/testing"

from mendeley import Mendeley

# These values should match the ones supplied when registering your application.
mendeley = Mendeley(client_id, redirect_uri=redirect_uri)

auth = mendeley.start_implicit_grant_flow()

# The user needs to visit this URL, and log in to Mendeley.
login_url = auth.get_login_url()

import requests

res = requests.post(login_url, allow_redirects = False, data = {
    'username': 'xxxx@gmail.com',
    'password': 'xxxxx'
})

auth_response = res.headers['Location']

# After logging in, the user will be redirected to a URL, auth_response.
session = auth.authenticate(auth_response)

print(session.files.list().items)

最后一行打印[<mendeley.models.files.File object at 0x1115b5400>],这意味着如果工作的话访问

相关问题 更多 >