Skip to content

Instantly share code, notes, and snippets.

@nathanhere
Created May 9, 2013 08:08
Show Gist options
  • Save nathanhere/5546176 to your computer and use it in GitHub Desktop.
Save nathanhere/5546176 to your computer and use it in GitHub Desktop.
Snippet used to successfully OAuth into the Linkedin API. Uses python-linkedin, requests, and a local Flask server to grab the authentication token from the preauthorization redirect URL containing the AUTH_CODE.
# Currently for use with local flask server only -- See corresponding flask code snippet
from linkedin import linkedin
import requests
API_KEY = ''
API_SECRET = ''
OAUTH_TOKEN = ''
OAUTH_SECRET= ''
SCOPE = 'r_fullprofile%20r_network%20r_emailaddress%20r_contactinfo'
PERMISSIONS = ['r_fullprofile', 'r_network', 'r_emailaddress','r_contactinfo']
REDIRECT_URI = 'http://127.0.0.1:5000/linkedinauth/authcode'
AUTH_CODE = ''
def auth(API_KEY, SCOPE, STATE, REDIRECT_URI):
return requests.Request('GET', 'https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id={0}&scope={1}&state={2}&redirect_uri={3}'.format(API_KEY. SCOPE, STATE,REDIRECT_URI))
def auth2(AUTH_CODE, REDIRECT_URI, API_KEY, API_SECRET):
return requests.post('https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code={0}&redirect_uri={1}&client_id={2}&client_secret={3}'.format(AUTH_CODE, REDIRECT_URI, API_KEY, API_SECRET))
def authnow():
with open('authcode.txt','r') as f:
AUTH_CODE = f.readline()
authentication.authorization_code = AUTH_CODE
authentication.get_access_token()
authentication = linkedin.LinkedInAuthentication(API_KEY, API_SECRET, REDIRECT_URI, PERMISSIONS)
# Click on the link provided by the print statement below. It will redirect to the
# flask server
print authentication.authorization_url
application = linkedin.LinkedInApplication(authentication)
# After webpage authentication has taken place:
authnow()
# Upon successful authorization, API requests can be made with the application object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment