Skip to content

Instantly share code, notes, and snippets.

@aveek22
Created March 12, 2022 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aveek22/824d964619d035a678cbb9462cb1312a to your computer and use it in GitHub Desktop.
Save aveek22/824d964619d035a678cbb9462cb1312a to your computer and use it in GitHub Desktop.
LinkedIn REST API
url_access_token = "https://www.linkedin.com/oauth/v2/accessToken"
auth_code = "YOUR_AUTH_CODE_FROM_PREVIOUS_VERIFICATION"
payload = {
'grant_type' : 'authorization_code',
'code' : auth_code,
'redirect_uri' : redirect_uri,
'client_id' : client_id,
'client_secret' : client_secret
}
response = requests.post(url=url_access_token, params=payload)
response_json = response.json()
# Extract the access_token from the response_json
access_token = response_json['access_token']
base_url = "https://www.linkedin.com/oauth/v2/authorization"
redirect_uri = "https://thedatascholar.com/auth/linkedin/callback"
scope = "w_member_social,r_liteprofile"
url = f"{base_url}?response_type=code&client_id={client_id}&state=random&redirect_uri={redirect_uri}&scope={scope}"
print(url)
# Get LinkedIn user ID
url = "https://api.linkedin.com/v2/me"
header = {
'Authorization' : f'Bearer {access_token}'
}
response = requests.get(url=url, headers=header)
response_json_li_person = response.json()
person_id = response_json_li_person['id']
url = "https://api.linkedin.com/v2/shares"
headers = {
'Authorization' : f'Bearer {access_token}',
'Content-Type' : 'application/json'
}
payload = {
"content": {
"contentEntities": [
{
"entityLocation": "https://www.redhat.com/en/topics/api/what-is-a-rest-api",
"thumbnails": [
{
"resolvedUrl": "https://images.pexels.com/photos/2115217/pexels-photo-2115217.jpeg"
}
]
}
],
"title": "What is a REST API?"
},
'distribution': {
'linkedInDistributionTarget': {}
},
'owner': f'urn:li:person:{person_id}',
'text': {
'text': f'Learn more about REST APIs in details. \n#restapi #api'
}
}
response = requests.post(url=url, headers=headers, json = payload)
print(response.json())
import requests # To deal with REST APIs
# Get the ClientID and ClientSecret to get the access token
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
organization_id = "YOUR_ORGANIZATION_ID"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment