Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 824
user2069136 Avatar asked Jul 17 '15 17:07

user2069136


3 Answers

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."
like image 157
Akanksha Singla Avatar answered Oct 19 '22 19:10

Akanksha Singla


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

linkedinapioauth2python

like image 37
cgoldie Avatar answered Oct 19 '22 18:10

cgoldie


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')
like image 1
Tom Quirk Avatar answered Oct 19 '22 19:10

Tom Quirk