简体   繁体   中英

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. Ie 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 (ie 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?

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

Updated for Python 3:

#%%### 3. LinkedIn API Work

import oauth2 as oauth
import urllib

consumer_key='XXXXXX' #from Linkedin site
consumer_secret='XXXXXX' #from Linkedin site
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'])
content_utf8=str(content,'utf-8') #convert binary to utf-8 string
request_token=dict(urllib.parse.parse_qsl(content_utf8))
authorize_url=request_token['xoauth_request_auth_url']

print("Go to the following link in your browser:", "\n")
print(authorize_url+'?oauth_token='+request_token['oauth_token'])

accepted='n'
while accepted.lower()=='n':
    accepted=input('Have you authorized me? (y/n)') #prompt for input (y)
oauth_verifier=input('What is the PIN?') #prompt for 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")
content8=str(content,'utf-8')
access_token = dict(urllib.parse.parse_qsl(content8))

print("Access Token:", "\n")
print("- oauth_token        = "+access_token['oauth_token']+'\n')
print("- oauth_token_secret = "+access_token['oauth_token_secret'])
print("You may now access protected resources using the access tokens above.")

It looks like a lot of these answers might be out of date.

Given it looks like you aren't tied to a specific project, I would recommend you take a look at https://github.com/tomquirk/linkedin-api (disclaimer: I maintain it).

You can authenticate with LinkedIn with the following:

from linkedin_api import LinkedIn

# Authenticate using any LinkedIn account credentials
api = Linkedin('youremail@gmail.com', 'l33tpassword')

After that, you can go ahead and perform requests, for email, get a LinkedIn profile:

profile = api.get_profile('profile-id')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM