How to use linkedin API with python
Asked Answered
C

2

19

I tried so many methods, but none seem to work. Help me make a connection with linkedin using python. I have all the tokens. I have python 2.7.5. Please post a sample of basic code that establishes a connection and gets a user's name.

Below, I have done character for character like the example said, but it doesn't work.

https://github.com/ozgur/python-linkedin <---This is where I got the api and I copied it exactly. See below:

CONSUMER_KEY = '9puxXXXXXXXX'     # This is api_key
CONSUMER_SECRET = 'brtXoXEXXXXXXXX'   # This is secret_key

USER_TOKEN = '27138ae8-XXXXXXXXXXXXXXXXXXXXXXXXXXX'   # This is oauth_token
USER_SECRET = 'ca103e23XXXXXXXXXXXXXXXXXXXXXXXXXXX'   # This is oauth_secret


from linkedin import linkedin

# Define CONSUMER_KEY, CONSUMER_SECRET,  
# USER_TOKEN, and USER_SECRET from the credentials 
# provided in your LinkedIn application

# Instantiate the developer authentication class

authentication = linkedin.LinkedInDeveloperAuthentication(CONSUMER_KEY, CONSUMER_SECRET, 
                                                      USER_TOKEN, USER_SECRET, 
                                                      RETURN_URL, linkedin.PERMISSIONS.enums.values())

# Pass it in to the app...

application = linkedin.LinkedInApplication(authentication)

# Use the app....

application.get_profile()

I get this error:

Traceback (most recent call last):
  File "C:/Documents and Settings/visolank/Desktop/Python/programs/linkedinapi.py", line 8, in <module>
    from linkedin import linkedin
  File "C:/Documents and Settings/visolank/Desktop/Python/programs\linkedin\linkedin.py", line 2, in <module>
    from requests_oauthlib import OAuth1
  File "C:\Python27\lib\site-packages\requests_oauthlib\__init__.py", line 1, in <module>
    from .core import OAuth1
  File "C:\Python27\lib\site-packages\requests_oauthlib\core.py", line 4, in <module>
from oauthlib.oauth1 import (Client, SIGNATURE_HMAC, SIGNATURE_TYPE_AUTH_HEADER)
  File "C:\Python27\lib\site-packages\requests_oauthlib\oauthlib\oauth1\__init__.py", line 12, in <module>
    from .rfc5849 import Client
  File "C:\Python27\lib\site-packages\requests_oauthlib\oauthlib\oauth1\rfc5849\__init__.py", line 26, in <module>
    from oauthlib.common import Request, urlencode, generate_nonce
ImportError: No module named oauthlib.common
Cursor answered 11/7, 2013 at 17:57 Comment(2)
It looks like to me that it is saying you missing the oauthlib module. Do you know if you have it installed? I think it might require installing properly oauth2Ovarian
I would recommend some people to read the answer here #31481772Buchan
C
15

Got it. For future reference you need to download the oauthlib from here https://github.com/idan/oauthlib

here is the full functional code:

CONSUMER_KEY = '9pux1XcwXXXXXXXXXX'     # This is api_key
CONSUMER_SECRET = 'brtXoXEXXXXXXXXXXXXX'   # This is secret_key

USER_TOKEN = '27138ae8-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXb'   # This is oauth_token
USER_SECRET = 'ca103e23-XXXXXXXXXXXXXXXXXXXXXXXX7bba512625e'   # This is oauth_secret
RETURN_URL = 'http://localhost:8000'

from linkedin import linkedin
from oauthlib import *

# Define CONSUMER_KEY, CONSUMER_SECRET,  
# USER_TOKEN, and USER_SECRET from the credentials 
# provided in your LinkedIn application

# Instantiate the developer authentication class

authentication = linkedin.LinkedInDeveloperAuthentication(CONSUMER_KEY, CONSUMER_SECRET, 
                                                      USER_TOKEN, USER_SECRET, 
                                                      RETURN_URL, linkedin.PERMISSIONS.enums.values())

# Pass it in to the app...

application = linkedin.LinkedInApplication(authentication)

# Use the app....

g = application.get_profile()
print g
Cursor answered 11/7, 2013 at 18:5 Comment(7)
Or you can just use the built-in OAuth2, as the docs suggest. If you can't get an OAuth2 app key, or just dislike OAuth2 for the usual reasons, this is the right answer; otherwise, it's unnecessary.Mezuzah
It just prints my profile data, how do I get it to print the auth url and then users can log into that and give me auth_code which I can then pass to the app to get their data?Krone
It seems that all LinkedIn is showing now in the Application page is a Client ID and a Client Secret code. There's no User token or User secret.Gong
Where is the token user and the token secret?Garnet
see the answer in this thread #31481772Buchan
How do you get outh token? I am getting error {"error_description":"missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : appId or redirect uri does not match authorization code or authorization code expired","error":"invalid_request"}Verein
I get Message File Name Line Position LinkedInError: 401 Client Error: Unauthorized for url: api.linkedin.com/v1/people/~: Unknown ErrorPlayground
P
1

You can get USER_TOKEN & USER_SECRET in such way (if you have CONSUMER_KEY & CONSUMER_SECRET):

import oauth2 as oauth
import urllib

consumer_key = '' #from Linkedin site
consumer_secret = '' #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')
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)')
oauth_verifier = 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')
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.')
Postulate answered 27/8, 2018 at 11:0 Comment(7)
Getting An Error occurred during authorization, please try again later. when I visit the authorize_url concatenated with the oauth_token. Between, I have replaced my CLIENT_ID by the consumer_key and CLIENT_SECRET by the consumer_secret.Cutcherry
Where to get consumer_key & consumer_secret from? Suppose if I want to practice scraping my own LinkedIn account?Rising
@AakashBasu you can find information about that on LinkedIn API documentation page - learn.microsoft.com/en-us/linkedin/shared/authentication/…Postulate
It wants details on App Logo, App name, Company, etc. But I am just going to test using Python. What should I fill there?Rising
I got the Consumer Key and Secret key, but 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?Rising
@AakashBasu if you work with Python I'd like to suggest you to use Python's wrapper-library for Linkedin API - pypi.org/project/python-linkedin-v2Postulate
@GlebKosteiko, In the example of the above link, it needs CONSUMER_KEY, CONSUMER_SECRET which I have but also needs USER_TOKEN, and USER_SECRET which I do not have. Where to get the latter two from?Rising

© 2022 - 2024 — McMap. All rights reserved.