-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkedin_integration.py
92 lines (77 loc) · 3.07 KB
/
linkedin_integration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import requests
from requests_oauthlib import OAuth2Session
# LinkedIn API credentials
CLIENT_ID = 'your_client_id'
CLIENT_SECRET = 'your_client_secret'
REDIRECT_URI = 'your_redirect_uri'
# Authorization URL
AUTHORIZATION_URL = 'https://www.linkedin.com/oauth/v2/authorization'
# Access token URL
TOKEN_URL = 'https://www.linkedin.com/oauth/v2/accessToken'
# Redirect URL for authorization
REDIRECT_URL = 'http://localhost:8000/callback'
# Scopes for LinkedIn API
SCOPES = ['r_liteprofile', 'r_emailaddress', 'w_member_social']
# Callback route for authorization
def callback(request):
# Get authorization code from the request
code = request.GET.get('code')
# Exchange authorization code for access token
token = get_access_token(code)
# Store the access token securely for future use
# Redirect to the desired page
# Get authorization URL
def get_authorization_url():
linkedin = OAuth2Session(CLIENT_ID, redirect_uri=REDIRECT_URI, scope=SCOPES)
authorization_url, state = linkedin.authorization_url(AUTHORIZATION_URL)
return authorization_url
# Get access token using authorization code
def get_access_token(code):
linkedin = OAuth2Session(CLIENT_ID, redirect_uri=REDIRECT_URI, scope=SCOPES)
token = linkedin.fetch_token(TOKEN_URL, code=code, client_secret=CLIENT_SECRET)
return token
# Retrieve player profile from LinkedIn
def get_player_profile(access_token):
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get('https://api.linkedin.com/v2/me', headers=headers)
player_profile = response.json()
return player_profile
# Update player profile on LinkedIn
def update_player_profile(access_token, player_data):
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json'}
response = requests.patch('https://api.linkedin.com/v2/me', headers=headers, json=player_data)
if response.status_code == 200:
return True
else:
return False
# Send message to a LinkedIn user
def send_message(access_token, recipient_id, message):
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json'}
payload = {
'recipients': {
'values': [{'person': {'_path': f'/people/{recipient_id}'}}]
},
'message': {
'subject': 'New message',
'body': message
}
}
response = requests.post('https://api.linkedin.com/v2/conversations', headers=headers, json=payload)
if response.status_code == 201:
return True
else:
return False
# Establish connection with a LinkedIn user
def establish_connection(access_token, profile_id):
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json'}
payload = {
'invitee': {
'profileId': profile_id
},
'message': 'Hi, I would like to connect with you on LinkedIn.'
}
response = requests.post('https://api.linkedin.com/v2/invitation', headers=headers, json=payload)
if response.status_code == 201:
return True
else:
return False