python-linkedin api - how do I use it?
Asked Answered
R

3

7

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?

Rosemaria answered 17/7, 2015 at 17:15 Comment(3)
Please post a copy of the actual error message.Anglaangle
@Anglaangle Of course, should have done that. Now it´s updated. Do you have a clue in how to do this?Rosemaria
I have an error in linkedin.PERMISSIONS.enums.values() is It need an import?Paronomasia
K
10

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."
Kakalina answered 17/3, 2016 at 10:39 Comment(3)
this works for me and should be the selected answer, thanksBrashear
This works for me as well. But I am stuck on how to use the access token to get list of all my connections. Can you please help?Descend
When I try to register it wants details on App Logo, App name, Company, etc. But I am just going to test using Python. What should I fill there? Any easier way to get data from my own profile?Heater
H
4

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

Harlie answered 4/4, 2018 at 16:7 Comment(2)
When I click on the link generated for authorization, it fails to open a page and says: Page not found Uh oh, we can’t seem to find the page you’re looking for. Try going back to the previous page or see our Help Center for more information. Where am I going wrong?Heater
What is pin here ?Recidivism
C
1

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('[email protected]', 'l33tpassword')

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

profile = api.get_profile('profile-id')
Cathode answered 9/9, 2020 at 12:7 Comment(2)
Thanks for sharing. How could I use this to get profile data from others? The profile owners would give me their consent for the data, but not sure if the API could do this?Chanson
@Tom, get_user_profile is not working either. Can you please look into it?Gayle

© 2022 - 2024 — McMap. All rights reserved.