Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

python-linkedin api - how do I use it?

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-linkedin library but cannot do it. According to Ozgur's page https://github.com/ozgur/python-linkedin I should be able to open the link generated from the .authorization_url function but that doesn′t work as I get an error saying that my redirect link is wrong even though I have entered it in my application at LinkedIn's developer page. I.e. when trying to open the link that the .authorization_url function gives, what shows up in the browser is the following error message:

"invalid redirect_uri. This value must match a URL registered with the API Key."

What I′m expecting is a page where I can approve access to my account. Can I, as in the code below have localhost:8000 (as Ozgur's page shows) as redirect link or what kind of link does it have to be? Can it be whatever?

from linkedin import linkedin
import webbrowser

API_KEY = '********'
API_SECRET = '*******'
RETURN_URL = 'http://localhost:8000'

authentication = linkedin.LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL, linkedin.PERMISSIONS.enums.values())
print (authentication.authorization_url)  # open this url on your browser
webbrowser.open(authentication.authorization_url)
application = linkedin.LinkedInApplication(authentication)
authentication.authorization_code = '4CqONljAz622ZBo0'
authentication.get_access_token()

Exactly how do I do this?

One more question, the above refers to using Oauth2, but it should still be possible to use Oauth1 according to their developers page, and not yet deprecated. However, for using Oauth1 one needs four different keys, the ones mostly referred to:

CONSUMER_KEY, CONSUMER_SECRET, USER_TOKEN, USER_SECRET

However from the application page (i.e. LinkedIn's, where one registeres the application) I can only find two: ClientID and Client_SECRET, which are for Oauth2. Does that mean it is not possible to use oauth1 anyway?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 "
"

request_token = dict(urlparse.parse_qsl(content))

print "Requesr Token:",  "
"
print "- oauth_token        = %s" % request_token['oauth_token'], "
"
print "- oauth_token_secret = %s" % request_token['oauth_token_secret'], "
"

authorize_url = 'https://api.linkedin.com/uas/oauth/authorize'
print "Go to the following link in your browser:", "
"
print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token']), "
"

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:", "
"
print "- oauth_token        = %s" % access_token['oauth_token'], "
"
print "- oauth_token_secret = %s" % access_token['oauth_token_secret']
print "You may now access protected resources using the access tokens above."

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...