Mendeley / mendeley-python-sdk

Python SDK for the Mendeley API.
Apache License 2.0
119 stars 59 forks source link

Authentication error #9

Closed hadim closed 9 years ago

hadim commented 9 years ago

Using this code (adapted from https://github.com/Mendeley/mendeley-api-python-catalog-example/blob/master/mendeley-catalog.py) :

from mendeley import Mendeley

mendeley = Mendeley("999", "XXXXXXXX")

session = mendeley.start_client_credentials_flow().authenticate()

I have the following errors :

---------------------------------------------------------------------------
MissingTokenError                         Traceback (most recent call last)
<ipython-input-15-05641665fe4b> in <module>()
      3 mendeley = Mendeley("XXX", "XXXXX")
      4 
----> 5 session = mendeley.start_client_credentials_flow().authenticate()

/home/hadim/local/conda/envs/st/lib/python3.4/site-packages/mendeley/auth.py in authenticate(self)
     29         oauth.compliance_hook['access_token_response'] = [handle_text_response]
     30 
---> 31         token = oauth.fetch_token(self.token_url, auth=self.auth, scope=['all'])
     32         return MendeleySession(self.mendeley,
     33                                token,

/home/hadim/local/conda/envs/st/lib/python3.4/site-packages/requests_oauthlib/oauth2_session.py in fetch_token(self, token_url, code, authorization_response, body, auth, username, password, method, timeout, headers, verify, **kwargs)
    197             r = hook(r)
    198 
--> 199         self._client.parse_request_body_response(r.text, scope=self.scope)
    200         self.token = self._client.token
    201         log.debug('Obtained token %s.', self.token)

/home/hadim/local/conda/envs/st/lib/python3.4/site-packages/oauthlib/oauth2/rfc6749/clients/base.py in parse_request_body_response(self, body, scope, **kwargs)
    407         .. _`Section 7.1`: http://tools.ietf.org/html/rfc6749#section-7.1
    408         """
--> 409         self.token = parse_token_response(body, scope=scope)
    410         self._populate_attributes(self.token)
    411         return self.token

/home/hadim/local/conda/envs/st/lib/python3.4/site-packages/oauthlib/oauth2/rfc6749/parameters.py in parse_token_response(body, scope)
    374 
    375     params = OAuth2Token(params, old_scope=scope)
--> 376     validate_token_parameters(params)
    377     return params
    378 

/home/hadim/local/conda/envs/st/lib/python3.4/site-packages/oauthlib/oauth2/rfc6749/parameters.py in validate_token_parameters(params)
    384 
    385     if not 'access_token' in params:
--> 386         raise MissingTokenError(description="Missing access token parameter.")
    387 
    388     if not 'token_type' in params:

MissingTokenError: (missing_token) Missing access token parameter.

Now if I use this code (from http://mendeley-python.readthedocs.org/en/latest/usage.html#authorization-code-flow) :

from mendeley import Mendeley

# These values should match the ones supplied when registering your application.
mendeley = Mendeley("XXXXX", client_secret="XXXX", redirect_uri="https://localhost")

auth = mendeley.start_authorization_code_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)

I have the following error :


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-3d0b0b562bf9> in <module>()
     10 
     11 # After logging in, the user will be redirected to a URL, auth_response.
---> 12 session = auth.authenticate(auth)

/home/hadim/local/conda/envs/st/lib/python3.4/site-packages/mendeley/auth.py in authenticate(self, redirect_url)
     66                                        authorization_response=redirect_url,
     67                                        auth=self.auth,
---> 68                                        scope=['all'])
     69 
     70         return MendeleySession(self.mendeley,

/home/hadim/local/conda/envs/st/lib/python3.4/site-packages/requests_oauthlib/oauth2_session.py in fetch_token(self, token_url, code, authorization_response, body, auth, username, password, method, timeout, headers, verify, **kwargs)
    154         if not code and authorization_response:
    155             self._client.parse_request_uri_response(authorization_response,
--> 156                     state=self._state)
    157             code = self._client.code
    158         elif not code and isinstance(self._client, WebApplicationClient):

/home/hadim/local/conda/envs/st/lib/python3.4/site-packages/oauthlib/oauth2/rfc6749/clients/web_application.py in parse_request_uri_response(self, uri, state)
    172             oauthlib.oauth2.rfc6749.errors.MismatchingStateError
    173         """
--> 174         response = parse_authorization_code_response(uri, state=state)
    175         self._populate_attributes(response)
    176         return response

/home/hadim/local/conda/envs/st/lib/python3.4/site-packages/oauthlib/oauth2/rfc6749/parameters.py in parse_authorization_code_response(uri, state)
    218         raise InsecureTransportError()
    219 
--> 220     query = urlparse.urlparse(uri).query
    221     params = dict(urlparse.parse_qsl(query))
    222 

/home/hadim/local/conda/envs/st/lib/python3.4/urllib/parse.py in urlparse(url, scheme, allow_fragments)
    290     Note that we don't break the components up in smaller bits
    291     (e.g. netloc is a single string) and we don't expand % escapes."""
--> 292     url, scheme, _coerce_result = _coerce_args(url, scheme)
    293     splitresult = urlsplit(url, scheme, allow_fragments)
    294     scheme, netloc, url, query, fragment = splitresult

/home/hadim/local/conda/envs/st/lib/python3.4/urllib/parse.py in _coerce_args(*args)
    110     if str_input:
    111         return args + (_noop,)
--> 112     return _decode_args(args) + (_encode_result,)
    113 
    114 # Result objects are more helpful than simple tuples

/home/hadim/local/conda/envs/st/lib/python3.4/urllib/parse.py in _decode_args(args, encoding, errors)
     94 def _decode_args(args, encoding=_implicit_encoding,
     95                        errors=_implicit_errors):
---> 96     return tuple(x.decode(encoding, errors) if x else '' for x in args)
     97 
     98 def _coerce_args(*args):

/home/hadim/local/conda/envs/st/lib/python3.4/urllib/parse.py in <genexpr>(.0)
     94 def _decode_args(args, encoding=_implicit_encoding,
     95                        errors=_implicit_errors):
---> 96     return tuple(x.decode(encoding, errors) if x else '' for x in args)
     97 
     98 def _coerce_args(*args):

AttributeError: 'MendeleyAuthorizationCodeAuthenticator' object has no attribute 'decode'

Any help would be great !

$ conda info
Current conda install:

             platform : linux-64
        conda version : 3.11.0
  conda-build version : 1.12.1
       python version : 3.4.3.final.0
     requests version : 2.6.2
     root environment : /home/hadim/local/conda  (writable)
  default environment : /home/hadim/local/conda/envs/st
     envs directories : /home/hadim/local/conda/envs
        package cache : /home/hadim/local/conda/pkgs
         channel URLs : https://repo.continuum.io/pkgs/free/linux-64/
                        https://repo.continuum.io/pkgs/free/noarch/
                        https://repo.continuum.io/pkgs/pro/linux-64/
                        https://repo.continuum.io/pkgs/pro/noarch/
          config file : None
    is foreign system : False

$ pip freeze | grep mendeley
mendeley==0.3.2
hadim commented 9 years ago

The following code works :

mendeley = Mendeley(client_id, client_secret, redirect_uri=redirect_uri)

auth = mendeley.start_client_credentials_flow()
session = auth.authenticate()

However with this kind of authentication I can't access to my personal library and this is what I would like to do...

hadim commented 9 years ago

@matt-thomson

matt-thomson commented 9 years ago

Hi @hadim - I no longer work for Mendeley, so I can't help you with this one. @MendeleyStack should be able to take a look.

Joyce-Stack commented 9 years ago

Hey @hadim

I was able to recreate your first error using the exact same code. What fixed it for me was regenerating my client secret on the dev portal. Not ideal at the moment but can you please try that first and see if it works ?

Joyce-Stack commented 9 years ago

@hadim what exactly are you trying to do? The client credentials flow is only for use with querying the catalog. Have you read some of the documentation over at http://dev.mendeley.com/reference/topics/authorization_overview.html

hadim commented 9 years ago

start_client_credentials_flow works well but I need to access to my personal documents which I can't do with start_client_credentials_flow.

So I need start_authorization_code_flow()