ozgur / python-linkedin

Python interface to the LinkedIn API
http://ozgur.github.com/python-linkedin/
MIT License
894 stars 392 forks source link

LinkedInHTTPError - 401 Client Error: Unauthorized #46

Closed planetcrypton closed 10 years ago

planetcrypton commented 10 years ago

I've just 'discovered' your python-linkedin library earlier today and set it up and tried to make it work with Django. But as soon as I set the authorization-code and want to retrieve the access-token with it, I get an LinkedInHTTPError : 401 Client Error: Unauthorized.

Here's my code; I'm using two views, one that redirects to the LinkedIn authorization page, and one for the return-url:

from django.shortcuts import render, render_to_response, redirect
from django.http import HttpResponse

from linkedin import linkedin

LI_API_KEY="123abc"
LI_API_SECRET="xyz123"
LI_RETURN_URL = 'http://127.0.0.1:8000/skills/linkedin-authcode/'

def signup(request):

    # Instantiate the developer authentication class
    authentication = linkedin.LinkedInAuthentication(LI_API_KEY, LI_API_KEY,
                                                     LI_RETURN_URL, linkedin.PERMISSIONS.enums.values())
    return redirect( authentication.authorization_url )

def linkedin_authcode(request):

    auth_code = request.GET.get('code', None)
    state = request.GET.get('state', None)

    authentication = linkedin.LinkedInAuthentication(LI_API_KEY, LI_API_KEY,
                                                     LI_RETURN_URL, linkedin.PERMISSIONS.enums.values())
    authentication.authorization_code = auth_code
    access_token = authentication.get_access_token()
    print access_token

    return HttpResponse("Linked Auth Code return page")

The debug-data I receive is:

Request Method: GET Request URL: http://127.0.0.1:8000/skills/linkedin-authcode/?code=AQRandalotmore&state=4d64f8aa4b79a1fab3a73909a46bc322 Django Version: 1.6.2 Exception Type: LinkedInHTTPError Exception Value:
401 Client Error: Unauthorized Exception Location: /Library/Python/2.7/site-packages/linkedin/linkedin.py in get_access_token, line 123 Python Executable: /usr/bin/python Python Version: 2.7.5 Python Path:
['/Users/planetcrypton/Sites/coworkerdev', '/Library/Python/2.7/site-packages/pip-1.5.4-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages'] Server time: Wed, 7 May 2014 12:54:23 +0000

ozgur commented 10 years ago

Hello,

I believe it is related to #43.

According to Lee Fu, LinkedIn Developer responsible for the API, the problem is due to the latency between servers after obtaining the access token. He is suggesting the clients to put a slight delay after obtaining the access token.

https://developer.linkedin.com/comment/31320#comment-31320

When I put a 3 seconds delay before making .get_profile() call, I always end up getting my profile information.

planetcrypton commented 10 years ago

All right. I believe to have read that there's a maximum time for using the retrieved authorization-code as well..?? Do have any experience with a maximum time?

On Mon, May 12, 2014 at 2:54 PM, Özgür Vatansever notifications@github.comwrote:

Hello,

I believe it is related to #43https://github.com/ozgur/python-linkedin/pull/43.

According to Lee Fu, LinkedIn Developer responsible for the API, the problem is due to the latency between servers after obtaining the access token. He is suggesting the clients to put a slight delay after obtaining the access token.

https://developer.linkedin.com/comment/31320#comment-31320

When I put a 3 seconds delay before making .get_profile() call, I always end up getting my profile information.

— Reply to this email directly or view it on GitHubhttps://github.com/ozgur/python-linkedin/issues/46#issuecomment-42827899 .

Mvh Sjoerd Kok

ozgur commented 10 years ago

No, I haven't but it is said by the doc that it will be alive and usable for 60 days. Probably, you will need to redirect the user to the authorization url again as long as it expires.

planetcrypton commented 10 years ago

I'm still getting the 401 client-error, no matter how long I wait after receiving the authorization-code - either manually or by timer.sleep(). Though it only occurs when calling authentication.get_access_token() (as in my original example).

Might I experience another error..??

On Mon, May 12, 2014 at 3:19 PM, Özgür Vatansever notifications@github.comwrote:

No, I haven't but it is said by the doc that it will be alive and usable for 60 days. Probably, you will need to redirect the user to the authorization url again after as long as it expires.

— Reply to this email directly or view it on GitHubhttps://github.com/ozgur/python-linkedin/issues/46#issuecomment-42830414 .

Mvh Sjoerd Kok

planetcrypton commented 10 years ago

Sorry for wasting your time here! A plain old copy/paste error:

linkedin.LinkedInAuthentication(LI_API_KEY, LI_API_KEY,..)

should of course have been

linkedin.LinkedInAuthentication(LI_API_KEY, LI_API_SECRET,...)