Getting OAuth access token for LinkedIn using python-linkedin library

牧云@^-^@ 提交于 2019-12-11 11:40:37

问题


I'm trying to get the LinkedIn user access token using python-linkedin library with the following code. It's giving me access code but not directing to else part after getting the access_code.

from linkedin import linkedin
from lnkd.settings import LINKEDIN_CONSUMER_KEY, LINKEDIN_CONSUMER_SECRET, RETURN_URL
from django.http import HttpResponseRedirect, HttpResponse


def get_linkedin_token(request):

    authentication = linkedin.LinkedInAuthentication( 
                            LINKEDIN_CONSUMER_KEY, 
                            LINKEDIN_CONSUMER_SECRET,
                            RETURN_URL,
                            linkedin.PERMISSIONS.enums.values()
                    )
    access_code = request.GET.get('code')

    if code is None:
        application = linkedin.LinkedInApplication(authentication)
        return HttpResponseRedirect(authentication.authorization_url)
    else:    
        authentication.authorization_code = access_code
        access_token = authentication.get_access_token()
        return Httpresponse(access_token)

What am I doing wrong?


回答1:


I know how to connect doing it step by step, I allways use the OAuth steps and it works for me, tested for XING and Linkedin:

from rauth import OAuth1Service
import webbrowser

CLIENT_ID = 'your client ID'
CLIENT_SECRET = 'your client secret'
RETURN_URL = "http://localhost:8000"
BASE_URL = 'https://api.linkedin.com'
AUTHORIZATION_URL = BASE_URL +'/uas/oauth/authenticate'
REQUEST_TOKEN_URL = BASE_URL +'/uas/oauth/requestToken'
ACCESS_TOKEN_URL = BASE_URL + '/uas/oauth/accessToken'

linkedin = OAuth1Service(
                    name='linkedin',
                    consumer_key=CLIENT_ID,
                    consumer_secret=CLIENT_SECRET,
                    request_token_url=REQUEST_TOKEN_URL,
                    access_token_url=ACCESS_TOKEN_URL,
                    authorize_url=AUTHORIZATION_URL,
                    base_url=BASE_URL)

token, token_secret = linkedin.get_request_token(
    method='GET',
    params={'oauth_callback': 'oob'})

url = linkedin.get_authorize_url(token)
webbrowser.open(url)

pin = raw_input('PIN:')

session = linkedin.get_auth_session(
    token,
    token_secret,
    method='POST',
    data={'oauth_verifier': pin})

Now, you have a variable called 'session' which allows to handle GET, POST and PUT requests. For an instance, that is for Xing, I didn´t try it with Linkedin but it should be something like:

#Find all IDs from your contacts list    
search = "/v1/users/me/contact_ids"
res_ids = session.get(search,params={'format':'json'})
res_ids = res_ids.json()
print res_ids

# PUT a new webpage in your profil
res = session.put(
 '/v1/users/me/web_profiles/homepage',
 {'url[]': 'http://stackoverflow.com/questions/25183197'}
)


来源:https://stackoverflow.com/questions/25183197/getting-oauth-access-token-for-linkedin-using-python-linkedin-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!