Search code examples
pythonlinkedin-api

Linkedin API: unable to get the access tokens using Linkedin API in python


I am trying to retrieve access tokens using Linkedin API. I use the linkedin package for python.

from linkedin import linkedin

API_KEY = '*****'

API_SECRET = '******'

RETURN_URL = 'http://localhost:8080/hello' #(this works ok)


authentication = linkedin.LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL)

print authentication.authorization_url  # open this url on your browser
application = linkedin.LinkedInApplication(authentication)

authentication.authorization_code = 'the code obtained from login in in previous link'

authentication.get_access_token()

I get the following error:

---------------------------------------------------------------------------
LinkedInError                             Traceback (most recent call last)
<ipython-input-26-a0063ada08a2> in <module>()
----> 1 authentication.get_access_token()

C:\Users\soledad.galli\AppData\Local\Continuum\Anaconda2\lib\site-packages\linkedin\linkedin.py in get_access_token(self, timeout)
113               'client_secret': self.secret}
114         response = requests.post(self.ACCESS_TOKEN_URL, data=qd, timeout=timeout, verify=False)
--> 115         raise_for_error(response)
116         response = response.json()
117         self.token = AccessToken(response['access_token'], response['expires_in'])

C:\Users\soledad.galli\AppData\Local\Continuum\Anaconda2\lib\site-packages\linkedin\utils.pyc in raise_for_error(response)
 61                 message = '%s: %s' % (response.get('error', error.message),
 62                                       response.get('error_description', 'Unknown Error'))
---> 63                 raise LinkedInError(message)
 64             else:
 65                 raise LinkedInError(error.message)


LinkedInError: invalid_request: missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : authorization code not found

I have read somewhere else, that this might be because the access tokens have a very short life, however, I manage to copy and paste quite quickly and still get the same error. Any idea why or how to resolve this?

Many thanks


Solution

  • I had the same problem and it proved I was pasting a wrong Authorization Code. I don't think it can expire so soon in order not to be able to used, but to be able to use it as soon as you get I change the code to be able to paste it:

    authentication.authorization_url  # open this url on your browser
    print authentication.authorization_url
    application = linkedin.LinkedInApplication(authentication)
    code = raw_input("code = ")
    authentication.authorization_code = code
    print authentication.get_access_token()
    

    In my case the redirect_uri is on local host where I have an index.php in the web root which prints out the code you need to paste in python script. The index.php is this:

    echo htmlspecialchars($_GET["code"]);
    

    Hope this will work for you. After struggling myself for a solution it came up it was not so complicated.