Saturday, May 23, 2015

Learning oauth2 with linkedin using python script

Today, we are going to learn oauth2 with linkedin using python. It's a very simple and easy task. Let's install the necessary library dependency. In debian, you just will need to install these two dependency packages, that is python-requests-oauthlib and python-oauthlib.

If you want to be sure the module are installed, you can start a python interactive shell, read below:

 user@localhost:~$ python  
 Python 2.7.9 (default, Apr 29 2015, 18:34:06)   
 [GCC 4.9.2] on linux2  
 Type "help", "copyright", "credits" or "license" for more information.  
 >>> from requests_oauthlib import OAuth2Session  
 >>> from requests_oauthlib.compliance_fixes import linkedin_compliance_fix  

Looks good, there is no error. If you do not have apt package manager, you can alternatively use pip to install, such as the following:

 user@localhost:~$ sudo pip install requests requests_oauthlib  
 Downloading/unpacking requests  
  Downloading requests-2.7.0.tar.gz (451kB): 451kB downloaded  
  Running egg_info for package requests  
 Downloading/unpacking requests-oauthlib  
  Downloading requests-oauthlib-0.5.0.tar.gz (54kB): 54kB downloaded  
  Running egg_info for package requests-oauthlib  
 Downloading/unpacking oauthlib>=0.6.2 (from requests-oauthlib)  
  Downloading oauthlib-0.7.2.tar.gz (106kB): 106kB downloaded  
  Running egg_info for package oauthlib  
 Installing collected packages: requests, requests-oauthlib, oauthlib  
  Running install for requests  
  Running install for requests-oauthlib  
  Running install for oauthlib  
 Successfully installed requests requests-oauthlib oauthlib  
 Cleaning up...  

Okay, so you are set, now let's create a python script now for the oauth2.

1:  #!/usr/bin/python  
4:  # Credentials you get from registering a new application  
5:  client_id = '***********'  
6:  client_secret = '**********'  
8:  # OAuth endpoints given in the LinkedIn API documentation  
9:  authorization_base_url = ''  
10:  token_url = ''  
12:  from requests_oauthlib import OAuth2Session  
13:  from requests_oauthlib.compliance_fixes import linkedin_compliance_fix  
15:  linkedin = OAuth2Session(client_id, redirect_uri='')  
16:  linkedin = linkedin_compliance_fix(linkedin)  
18:  # Redirect user to LinkedIn for authorization  
19:  authorization_url, state = linkedin.authorization_url(authorization_base_url)  
20:  print 'Please go here and authorize,', authorization_url  
22:  # Get the authorization verifier code from the callback url  
23:  redirect_response = raw_input('Paste the full redirect URL here:')  
25:  linkedin.fetch_token(token_url, client_secret=client_secret, authorization_response=redirect_response)  
27:  # Fetch a protected resource, i.e. user profile  
28:  r = linkedin.get('')  
29:  print r.content  

There are basically two variable value you need to change, client_id and client_secret. You should be able to find these two variable in your linkedin developer application page. Once you login to your developer login page, you should be able to locate these two parameters. Note that, because of privacy of credential, I have remove them all, but you should get be able to find out. Note also you need to provide a redirect url, as seen here, it is match with what it is found in the script.

Now, we are almost ready. If you execute this script in the terminal,

 user@localhost:~$ python  
 Please go here and authorize,**********&redirect_uri=http%3A%2F%2F127.0.0.1&state=*************  
 Paste the full redirect URL here:******************************************************************************************************************************************&state=***************  
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  <headline>Software Engineer</headline>  

So you should copy the first link in the script output into the browser, a linkedin authorization page will be loaded, you should authorized it and the next url, copy and paste back into the terminal. Voila, you can use the token to start use various linkedin api.

1 comment:

  1. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.

    python training in bangalore|