Files
mymach_social_nextgen/extra-addons/mymach_social_nextgen/api/linkedin_api.py

37 lines
1.3 KiB
Python

import requests
from urllib.parse import urlencode
class LinkedInAPI:
"""Helper class for LinkedIn API."""
AUTH_URL = "https://www.linkedin.com/oauth/v2/authorization"
TOKEN_URL = "https://www.linkedin.com/oauth/v2/accessToken"
def __init__(self, client_id, client_secret, redirect_uri):
self.client_id = client_id
self.client_secret = client_secret
self.redirect_uri = redirect_uri
def get_auth_url(self, state):
params = {
'response_type': 'code',
'client_id': self.client_id,
'redirect_uri': self.redirect_uri,
'state': state,
'scope': 'w_member_social r_liteprofile w_organization_social r_organization_social'
}
return f"{self.AUTH_URL}?{urlencode(params)}"
def exchange_code_for_token(self, code):
data = {
'grant_type': 'authorization_code',
'code': code,
'client_id': self.client_id,
'client_secret': self.client_secret,
'redirect_uri': self.redirect_uri
}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post(self.TOKEN_URL, data=data, headers=headers, timeout=10)
response.raise_for_status()
return response.json()