python-linkedin api - how do I use it?

后端 未结 3 787
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 16:37

I know, questions regarding this have been asked before but I can´t find a solution. I Am trying to access my LinkedIn account through the supposedly simple to use python-li

3条回答
  •  一整个雨季
    2021-01-05 17:38

    U only need ClientID and Client_SECRET. The following code shall help u to get other two important keys. The access token keys shall be valid for 60 days. Use ouath2 anyway, The redirect url i choose is 'http://localhost:3000/auth/linkedin/callback'

    check it out

    import oauth2 as oauth
    import urlparse
    
    consumer_key           = "******"
    consumer_secret        = "******"
    consumer = oauth.Consumer(consumer_key, consumer_secret)
    client = oauth.Client(consumer)
    
    request_token_url      = 'https://api.linkedin.com/uas/oauth/requestToken'
    resp, content = client.request(request_token_url, "POST")
    if resp['status'] != '200':
        raise Exception("Invalid response %s." % resp['status'])
    
    print content
    print "\n"
    
    request_token = dict(urlparse.parse_qsl(content))
    
    print "Requesr Token:",  "\n"
    print "- oauth_token        = %s" % request_token['oauth_token'], "\n"
    print "- oauth_token_secret = %s" % request_token['oauth_token_secret'], "\n"
    
    authorize_url = 'https://api.linkedin.com/uas/oauth/authorize'
    print "Go to the following link in your browser:", "\n"
    print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token']), "\n"
    
    accepted = 'n'
    while accepted.lower() == 'n':
        accepted = raw_input('Have you authorized me? (y/n) ')
    oauth_verifier = raw_input('What is the PIN? ')
    
    access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken'
    token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
    token.set_verifier(oauth_verifier)
    client = oauth.Client(consumer, token)
    
    resp, content = client.request(access_token_url, "POST")
    access_token = dict(urlparse.parse_qsl(content))
    
    print "Access Token:", "\n"
    print "- oauth_token        = %s" % access_token['oauth_token'], "\n"
    print "- oauth_token_secret = %s" % access_token['oauth_token_secret']
    print "You may now access protected resources using the access tokens above."
    

提交回复
热议问题