-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrakt_v2_oauth.py
65 lines (52 loc) · 2.59 KB
/
trakt_v2_oauth.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
#! /usr/bin/env python3
import datetime
import json
import os
import sys
import requests
import client_secret_holder
client_id = '18313245490a6414e3f46e981e263f845d28c716cd24a6c58137e89a869dfdcb'
local_storage_json_file = './imdb_to_trakt_v2_sync_token.json'
def get_access_token(oauth_pin=None):
if not os.path.isfile(local_storage_json_file) and oauth_pin is None:
# no local token was found and no pin was supplied
sys.exit('Please supply an OAuth trakt pin. See -h for instructions.')
if oauth_pin is not None:
# every time a PIN is supplied, try getting a new token
token_request = requests.post('https://api-v2launch.trakt.tv/oauth/token', json={
'code': oauth_pin,
'client_id': client_id,
'client_secret': client_secret_holder.get(),
'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
'grant_type': 'authorization_code'
})
if token_request.status_code == 200:
print('Successfully got token from supplied PIN.')
# save response to local json file
json.dump(token_request.json(), open(local_storage_json_file, 'w'))
else:
sys.exit('Exchanging code for token failed with http code %d.\n%s' %
(token_request.status_code, token_request.text))
tokens = json.load(open(local_storage_json_file))
expire_date = datetime.datetime.utcfromtimestamp(tokens['created_at'] + tokens['expires_in'])
remaining_time = expire_date - datetime.datetime.utcnow()
# make sure the token is at least valid for the next 60 seconds (should be enough runtime for the script)
if remaining_time < datetime.timedelta(seconds=60):
print('Token expired')
token_refresh_request = requests.post('https://api-v2launch.trakt.tv/oauth/token', json={
'refresh_token': tokens['refresh_token'],
'client_id': client_id,
'client_secret': client_secret_holder.get(),
'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
'grant_type': 'refresh_token'
})
if token_refresh_request.status_code == 200:
print('Successfully refreshed token')
# save response to local json file
json.dump(token_refresh_request.json(), open(local_storage_json_file, 'w'))
# reload new token
tokens = json.load(open(local_storage_json_file))
else:
sys.exit('Refreshing token failed with http code %d.\n%s' %
(token_refresh_request.status_code, token_refresh_request.text))
return tokens['access_token']