Authentication issue in mendeley Python SDK

447 views Asked by At

I was reading Mendeley docs from here. I am trying to get data in my console for which I am using the following code from the tutorial

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)

Now I don't understand where is auth_response will come from in the last line of code ? Does anybody have any idea ? Thanks

2

There are 2 answers

4
Tarun Lalwani On BEST ANSWER

I was able to experiment with and get it working using below code, fully automated. No user intervention

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': '[email protected]',
    '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)

The last line prints [<mendeley.models.files.File object at 0x1115b5400>] which means the access if working

0
Noah On

From what I can tell, auth_response is provided by the user / server. For example, I found this nice block of code on github (full source here: 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')

As you can see, the author seems to be redirecting the client back to the original request.url .