Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh token won´t work #10

Open
DavidWiesner opened this issue Jul 10, 2015 · 4 comments
Open

Refresh token won´t work #10

DavidWiesner opened this issue Jul 10, 2015 · 4 comments

Comments

@DavidWiesner
Copy link

When I use your mendeley-api-python-example and the access token gets expired I expected the access token gets refreshed automatically, but it won´t.

So here in are the steps to reproduce:

  1. install and run the mendeley-api-python-example
  2. wait until the token gets expired (1h)
  3. refresh website
expected result

the access_token will be refreshed with the refresh_token

actual result

TokenExpiredError

expected fix: using MendeleyAuthorizationCodeTokenRefresher

I expected to fix this bug by using MendeleyAuthorizationCodeTokenRefresher in mendeley-example.py#L101
So I change the method to:

def get_session_from_cookies():
    authenticator = MendeleyAuthorizationCodeAuthenticator(mendeley, session['state'])
    refresher = MendeleyAuthorizationCodeTokenRefresher(authenticator)
    return MendeleySession(mendeley, session['token'], refresher=refresher)
actual result

TokenExpiredError
and log: No new refresh token given. Re-using old.

possible fix in MendeleyAuthorizationCodeAuthenticator

the problem is oauth.refresh_token try to find a refresh token his object, but because the OAuth2Session object is recreated this token must be passed explicit:

--- auth.py (revision )
+++ auth.py (revision )
@@ -104,7 +104,7 @@
         self.redirect_uri = authenticator.mendeley.redirect_uri

     def refresh(self, session):
-        oauth = OAuth2Session(client=self.client, redirect_uri=self.redirect_uri, scope=['all'])
+        oauth = OAuth2Session(client=self.client, redirect_uri=self.redirect_uri, scope=['all'], token=session.token)
         oauth.compliance_hook['access_token_response'] = [handle_text_response]

         session.token = oauth.refresh_token(self.token_url, auth=self.auth)

This should be fixed the bug
regards,
david

@Joyce-Stack
Copy link
Contributor

David,

Have you been able to run this code successfully?

@Joyce-Stack
Copy link
Contributor

Is this still an issue or can I close this?

@mfschmidt
Copy link

mfschmidt commented May 3, 2016

@MendeleyStack: Rather than "closing" this, I suggest you make his edits to the source so we can all benefit downstream. The problem is easily replicable and easily solved, thanks to his well-written explanation, but only if you have credentials to fix the codebase.

Alternatively, please provide an alternative method for auto-refreshing expired tokens.

Thanks!

wrongu pushed a commit to wrongu/mendeley-python-sdk that referenced this issue Feb 5, 2017
wrongu pushed a commit to wrongu/mendeley-python-sdk that referenced this issue Feb 7, 2017
wrongu pushed a commit to wrongu/mendeley-python-sdk that referenced this issue Feb 7, 2017
@mebibou
Copy link

mebibou commented May 17, 2023

I dont know if it is related, but very regularly I get the following error:

ile \"/var/task/mendeley/auth.py\", line 31, in authenticate
    token = oauth.fetch_token(self.token_url, auth=self.auth, scope=['all'])
  File \"/var/task/requests_oauthlib/oauth2_session.py\", line 364, in fetch_token
    r = hook(r)
  File \"/var/task/mendeley/auth.py\", line 12, in handle_text_response
    if rsp.headers['content-type'] == 'text/plain':
  File \"/var/task/requests/structures.py\", line 52, in __getitem__
    return self._store[key.lower()][1]
KeyError: 'content-type'

I authenticate using:

mendel = Mendeley(CLIENT_ID, CLIENT_SECRET)
session = mendel.start_client_credentials_flow().authenticate()

But I imagine the error happens whenever the token expires. How to fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants