3

I am trying to follow some companies registered on LinkedIn through python code and as per LinkedIn API documentation I need to use oauth2 - POST method to follow a company.

My queries are below:

  1. How to specify a particular company name via python code to follow a company?
  2. Can someone advise the python code for this?

My code is below:

oauth_token    = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=api_key, secret=api_secret)
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
http_method    = "POST"
http_handler   = urllib.HTTPHandler(debuglevel=_debug)
https_handler  = urllib.HTTPSHandler(debuglevel=_debug)

def linkedinreq(url, method, parameters):
          req = oauth.Request.from_consumer_and_token(oauth_consumer,
                                            token=oauth_token,
                                            http_method=http_method,
                                            http_url=url, 
                                            parameters=parameters)

          req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)
          req.to_postdata()

def fetchsamples():
          url = "https://api.linkedin.com/v1/people/~/following/companies"

          parameters = []
          response = linkedinreq(url, "POST", parameters)

fetchsamples()

2 Answers 2

8

The python modules python-linkedin and python-linkedin-v2 are outdated. Thus, I suggest you to use the requests_oauthlib module instead.

from requests_oauthlib import OAuth2Session
from requests_oauthlib.compliance_fixes import linkedin_compliance_fix

# In case the `redirect_url` does not implement https     
import os                                                   
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' 

# Credentials you get from registering a new application
client_id = '<the client id you get from linkedin>'
client_secret = '<the client secret you get from linkedin>'
redirect_url = '<authorized redirect URL from LinkedIn config>'

# OAuth endpoints given in the LinkedIn API documentation (check for updates)
authorization_base_url = 'https://www.linkedin.com/oauth/v2/authorization'
token_url = 'https://www.linkedin.com/oauth/v2/accessToken'

# Authorized Redirect URL (from LinkedIn config)
linkedin = OAuth2Session(client_id, redirect_uri=redirect_url)
linkedin = linkedin_compliance_fix(linkedin)

# Redirect user to LinkedIn for authorization
authorization_url, state = linkedin.authorization_url(authorization_base_url)
print('Please go here and authorize,', authorization_url)

# Get the authorization verifier code from the callback url
redirect_response = input('Paste the full redirect URL here:')

# Fetch the access token
linkedin.fetch_token(token_url, client_secret=client_secret,
                     authorization_response=redirect_response)

# Fetch a protected resource, i.e. user profile
r = linkedin.get('https://api.linkedin.com/v1/people/~')
print(r.content)
Sign up to request clarification or add additional context in comments.

3 Comments

Where and how to get the client_id and client_secret from? Suppose if I want to practice fetching my own LinkedIn account info with this ethical procedure?
I would add the following to the authorization_url: authorization_url += "&scope=r_liteprofile"
But what do I need to do to automatically pass the redirect_response without having to input manually as input?
0

Instead of reinventing the wheel, use python-linkedin wrapper.

Example code to search for the companies:

from linkedin import linkedin

CONSUMER_KEY = 'your key'
CONSUMER_SECRET = 'your secret'
USER_TOKEN = 'your token'
USER_SECRET = 'your user secret'
RETURN_URL = ''

# 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)

print application.search_company(selectors=[{'companies': ['name', 'universal-name', 'website-url']}],
                                 params={'keywords': 'apple microsoft'})

To follow the company, use follow_company() method, see more information and examples here:

COMPANY_ID = 1035  # this you would get from the `search_company()`
application.follow_company(COMPANY_ID)

1 Comment

Thank you for replying. I came across this link and used it. However, I want to know regarding POST in oauth2. I could not find good tutorial to understand this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.