Проблема аутентификации в mendeley Python SDK
Я читал документы Менделея отсюда. Я пытаюсь получить данные в своей консоли, для которых я использую следующий код из учебника
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
придет из последней строки кода? У кого-нибудь есть идеи? Спасибо
2 ответа
Решение
Я смог поэкспериментировать и заставить его работать, используя приведенный ниже код, полностью автоматизированный. Нет вмешательства пользователя
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
предоставляется пользователем / сервером. Например, я нашел этот хороший блок кода на github (полный источник здесь: ссылка):
@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 .