Showing posts with label linkedin. Show all posts
Showing posts with label linkedin. Show all posts

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 setup.py egg_info for package requests  
 Downloading/unpacking requests-oauthlib  
  Downloading requests-oauthlib-0.5.0.tar.gz (54kB): 54kB downloaded  
  Running setup.py 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 setup.py egg_info for package oauthlib  
 Installing collected packages: requests, requests-oauthlib, oauthlib  
  Running setup.py install for requests  
  Running setup.py install for requests-oauthlib  
  Running setup.py 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  
2:    
3:    
4:  # Credentials you get from registering a new application  
5:  client_id = '***********'  
6:  client_secret = '**********'  
7:    
8:  # OAuth endpoints given in the LinkedIn API documentation  
9:  authorization_base_url = 'https://www.linkedin.com/uas/oauth2/authorization'  
10:  token_url = 'https://www.linkedin.com/uas/oauth2/accessToken'  
11:    
12:  from requests_oauthlib import OAuth2Session  
13:  from requests_oauthlib.compliance_fixes import linkedin_compliance_fix  
14:    
15:  linkedin = OAuth2Session(client_id, redirect_uri='http://127.0.0.1')  
16:  linkedin = linkedin_compliance_fix(linkedin)  
17:    
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  
21:    
22:  # Get the authorization verifier code from the callback url  
23:  redirect_response = raw_input('Paste the full redirect URL here:')  
24:    
25:  linkedin.fetch_token(token_url, client_secret=client_secret, authorization_response=redirect_response)  
26:    
27:  # Fetch a protected resource, i.e. user profile  
28:  r = linkedin.get('https://api.linkedin.com/v1/people/~')  
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 http://127.0.0.1 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 linkedin.py  
 Please go here and authorize, https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=**********&redirect_uri=http%3A%2F%2F127.0.0.1&state=*************  
 Paste the full redirect URL here:https://127.0.0.1/?code=******************************************************************************************************************************************&state=***************  
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
 <person>  
  <id>upgggg_g_0</id>  
  <first-name>Dan</first-name>  
  <last-name>Christensen</last-name>  
  <headline>Software Engineer</headline>  
  <site-standard-profile-request>  
   <url>https://www.linkedin.com/profile/view?id=22222222222&amp;authType=name&amp;authToken=abcde;trk=api*************</url>  
  </site-standard-profile-request>  
 </person>  

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.