【问题标题】:Authentication issue in mendeley Python SDKmendeley Python SDK中的身份验证问题
【发布时间】:2018-05-26 09:56:17
【问题描述】:

我正在阅读来自 here 的 Mendeley 文档。我正在尝试在我的控制台中获取数据,我正在使用教程中的以下代码

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 来自哪里?有人知道吗?谢谢

【问题讨论】:

  • 这是一个断开连接的身份验证流程,需要手动干预。您需要打印login_url,然后在您的浏览器中浏览它,这将给您一个auth_response。然后你在你的代码中使用它来进行身份验证。如果您需要有限的访问权限,您可以使用this
  • 我需要通过运行这样的脚本来自动更新我的文档,如果没有人工干预,这似乎是不可能的。我确实尝试过自动登录,但仍然无法正常工作。你对此有什么想法吗?
  • 你可以使用 selenium 为你做这件事。您可以为 selenium 添加代码以打开浏览器、导航 url、填写登录详细信息然后为您获取代码
  • 贴出答案,请看。这不需要任何硒或任何东西

标签: python oauth mendeley


【解决方案1】:

我能够使用以下代码进行试验并使其工作,完全自动化。无需用户干预

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>],这意味着访问如果工作正常

【讨论】:

  • 谢谢,我试过了,但在auth_response = res.headers['Location'] 行它说KeyError: 'location'。当我试图查看res.headers 时,也没有密钥location。我得到以下 dict 作为响应 {'Date': 'Mon, 12 Feb 2018 17:34:29 GMT', 'Content-Length': '90', 'X-Mendeley-Trace-Id': 'XXXXXXXXXX', 'Connection': 'keep-alive', 'Content-Type': 'application/json'}
  • 响应码<Response [400]>。之后 res.headers 返回上面的字典
  • 可以查看response.text的内容吗?您收到一些错误,可能是身份验证或其他消息。 400 表示错误的请求。另外我希望你使用的是你在 APP 中配置的重定向 url,而不是我列出的那个
  • 这是一个注册的 URI 错误。我在注册另一个时正在使用您的。非常感谢您的帮助。它现在可以工作了:)
【解决方案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')

如您所见,作者似乎是在将客户端重定向回原来的 request.url 。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 2015-01-15
    • 2018-01-27
    相关资源
    最近更新 更多