I am writing script for extracting data using linkedin_api . However while authenticating the app it redirects me to localhost.
import requests
import random
import json
import string
import webbrowser
def read_credentials(filename):
""""Reads the credentials and return the clientID and password"""
try:
with open(filename) as f:
credentials = json.load(f)
return credentials
except FileNotFoundError:
print(f'Error:{filename} not found')
return None
def create_CSRF_token():
"""Protects against cross site request forgery"""
letters = string.ascii_lowercase
tokens = ''.join(random.choice(letters) for i in range(20))
return tokens
api_url = 'https://www.linkedin.com/oauth/v2'
def authorize(api_url,client_id,client_secret,client_uri):
"""
Make a HTTP request to the authorization URL.
It will open the authentication URL.
Once authorized, it'll redirect to the redirect URI given.
The page will look like an error. but it is not.
You'll need to copy the redirected URL.
"""
# Request authentication url
csrf_token = create_CSRF_token()
params = {
'response_type' : 'code',
'client_id' : client_id,
'redirect_uri': client_uri,
'state': csrf_token,
'scope': 'r_liteprofile,r_emailaddress,w_member_social'
}
response = requests.get(f'{api_url}/authorization',params=params)
authorization_url = response.url
print(f'''
The authorization url is {authorization_url}
The Browser will open to ask you to authorize the credentials.n
Since we have not set up a server, you will get the error:n
This site can’t be reached. localhost refused to connect.n
This is normal.n
You need to copy the URL where you are being redirected to.n
''')
open_url(response.url)
# Get the authorization verifier code from the callback url
redirect_response = input('Paste the full redirect URL here:')
auth_code = parse_redirect_uri(redirect_response)
return auth_code
def open_url(url):
'''
Function to Open URL.
Used to open the authorization link
'''
print(url)
webbrowser.open(url)
def parse_redirect_uri(redirect_response):
"""Parse redirect responses into components.
Extract the authorized tokens from redirect uri"""
from urllib.parse import urlparse, parse_qs
url = urlparse(redirect_response)
query_params = parse_qs(url.query)
if 'code' in query_params:
return query_params['code'][0]
else:
print("Authorization code not found in redirect URL")
return None
def save_token(filename,data):
"""
Write tokens to credentials file
"""
data = json.dumps(data, indent=4)
with open(filename,'w') as f:
f.write(data)
def headers(access_token):
"""
Make the headers to attach to the API call
"""
headers = {
'authorization':f'Bearer{access_token}',
'cache-control': 'no-cache',
'X-Restli-Protocol-Version': '2.0.0'
}
return headers
def refresh_token(auth_code,client_id,client_secret,client_uri):
'''
Exchange a Refresh Token for a New Access Token.
'''
access_token_url = 'https://www.linkedin.com/oauth/v2/accessToken'
data = {
'grant_type': 'authorization_code',
'code': auth_code,
'redirect_uri': client_uri,
'client_id': client_id,
'client_secret': client_secret
}
response = requests.post(access_token_url, data=data, timeout=30)
response = response.json()
print(response)
access_token = response.get('access_token')
if access_token is None:
print('Access token not found in response data')
return access_token
def auth(credentials):
'''
Run the Authentication.
If the access token exists, it will use it to skip browser auth.
If not, it will open the browser for you to authenticate.
You will have to manually paste the redirect URI in the prompt.
'''
creds = read_credentials(credentials)
#print(creds)
client_id, client_secret = creds['client_id'], creds['client_secret']
redirect_uri = creds['redirect_uri']
api_url = 'https://www.linkedin.com/oauth/v2'
if 'access_token' not in creds.keys():
args = client_id,client_secret,redirect_uri
auth_code = authorize(api_url,*args)
access_token = refresh_token(auth_code,*args)
creds.update({'access_token':access_token})
save_token(credentials,creds)
else:
access_token = creds['access_token']
return access_token
if __name__ == '__main__':
credentials = 'credentials.json'
access_token = auth(credentials)
I am using the above code for authentication
Authorization code not found in redirect URL
{'error': 'invalid_request', 'error_description': 'A required parameter "code" is missing'}
Access token not found in response data
This is the output I am getting after running the code.
I have already checked for my credentials in json file, they match with the values on Linkedin developer portal.
What could be potential issue here ?
Authenticate the linkedin api. Expecting the access token to be generated after running the script.