5

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

muazfaiz
  • 4,611
  • 14
  • 50
  • 88
  • This is a disconnected auth flow which needs manual intervention. You need to print the `login_url` and then browse that in your browser which will then give you a `auth_response`. Then you use that in your code to authenticate. If you require limited access you can use [this](https://mendeley-python.readthedocs.io/en/latest/usage.html#client-credentials-flow) – Tarun Lalwani Feb 06 '18 at 05:14
  • I need to update my documents automatically by running a script like this and it seems like not possible without manual intervention. I did try login automatically but that still doesn't work. Do you have any idea about that? – muazfaiz Feb 06 '18 at 09:28
  • You can use selenium to do that for you. You can added code for selenium to open the browser, navigate the url, fill the login details and then get code for you – Tarun Lalwani Feb 06 '18 at 09:29
  • Posted the answer, please have a look. This doesn't need any selenium or anything – Tarun Lalwani Feb 12 '18 at 16:47

2 Answers2

6

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': '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)

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

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • 1
    Thanks I tried that but at line `auth_response = res.headers['Location']` it says `KeyError: 'location'`. When I tried to see the `res.headers` there was also no key `location`. I got the following dict as response `{'Date': 'Mon, 12 Feb 2018 17:34:29 GMT', 'Content-Length': '90', 'X-Mendeley-Trace-Id': 'XXXXXXXXXX', 'Connection': 'keep-alive', 'Content-Type': 'application/json'}` – muazfaiz Feb 12 '18 at 17:37
  • Response code ``. After that res.headers return the above dict – muazfaiz Feb 12 '18 at 17:58
  • Can you check the content of `response.text`? You are getting some error which may be auth or some other message. 400 means bad request. Also I hope you used the redirect url you configured in the APP and not the one i listed – Tarun Lalwani Feb 12 '18 at 17:59
  • It was a registered URI fault. I was using yours while I registered the other one. Thanks a lot man for the help. It works now :) – muazfaiz Feb 12 '18 at 18:51
1

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 .

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Noah
  • 51
  • 1
  • 4