/
pin.py
78 lines (55 loc) · 2.2 KB
/
pin.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
from __future__ import absolute_import, division, print_function
from trakt import Trakt
from six.moves import input
import logging
import os
logging.basicConfig(level=logging.DEBUG)
class Application(object):
def __init__(self):
self.authorization = None
# Bind trakt events
Trakt.on('oauth.token_refreshed', self.on_token_refreshed)
def run(self):
self.authenticate()
if not self.authorization:
print('Authentication required')
exit(1)
# Simulate expired token
self.authorization['expires_in'] = 0
# Test authenticated calls
with Trakt.configuration.oauth.from_response(self.authorization):
# Expired token, requests will return `None`
print(Trakt['sync/collection'].movies())
with Trakt.configuration.oauth.from_response(self.authorization, refresh=True):
# Expired token will be refreshed automatically (as `refresh=True`)
print(Trakt['sync/collection'].movies())
with Trakt.configuration.oauth.from_response(self.authorization):
# Current token is still valid
print(Trakt['sync/collection'].movies())
def authenticate(self):
# Request authentication
print('Navigate to %s' % Trakt['oauth/pin'].url())
pin = input('Pin: ')
# Exchange `code` for `access_token`
self.authorization = Trakt['oauth'].token_exchange(pin, 'urn:ietf:wg:oauth:2.0:oob')
if not self.authorization:
return False
print('Token exchanged - authorization: %r' % self.authorization)
return True
def on_token_refreshed(self, response):
# OAuth token refreshed, save token for future calls
self.authorization = response
print('Token refreshed - authorization: %r' % self.authorization)
if __name__ == '__main__':
# Configure
Trakt.base_url = 'http://api.staging.trakt.tv'
Trakt.configuration.defaults.app(
id=os.environ.get('APP_ID')
)
Trakt.configuration.defaults.client(
id=os.environ.get('CLIENT_ID'),
secret=os.environ.get('CLIENT_SECRET')
)
# Start application
app = Application()
app.run()