-
Notifications
You must be signed in to change notification settings - Fork 46
/
refresh_access_token.py
executable file
·60 lines (49 loc) · 2.22 KB
/
refresh_access_token.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
#!/usr/bin/env python
import argparse
import sys
from os.path import abspath, dirname, join
sys.path.append(abspath(join(dirname(__file__), "..", "src")))
from access_token import AccessToken
from api_config import ApiConfig
from arguments import common_arguments
from user import User
def main(argv=[]):
"""
This script refreshes an access token originally created by using the
the get_access_token.py script with the --write argument. The new access
token may be printed with the --cleartext option.
"""
parser = argparse.ArgumentParser(description="Refresh Pinterest OAuth token")
parser.add_argument(
"-ct", "--cleartext", action="store_true", help="print the token in clear text"
)
parser.add_argument(
"-c", "--continuous", action="store_true", help="continuous refresh token"
)
common_arguments(parser)
args = parser.parse_args(argv)
# get configuration from defaults and/or the environment
api_config = ApiConfig(verbosity=args.log_level)
access_token = AccessToken(api_config, name=args.access_token)
access_token.read()
access_token.refresh(continuous=args.continuous)
# Note: It is best practice not to print credentials in clear text.
# Pinterest engineers asked for this capability to make it easier
# to support partners.
if args.cleartext:
print("Please keep clear text tokens secure!")
print("clear text access token after refresh: " + access_token.access_token)
print("clear text refresh token after refresh: " + access_token.refresh_token)
print("hashed access token after refresh: " + access_token.hashed())
print("hashed refresh token after refresh: " + access_token.hashed_refresh_token())
# Use the access token to get information about the user. The purpose of this
# call is to verify that the access token is working.
user = User(api_config, access_token)
user_data = user.get()
user.print_summary(user_data)
print("writing access token")
access_token.write()
# If this script is being called from the command line, call the main function
# with the arguments. The other use case is to call main() from an integration test.
if __name__ == "__main__":
main(sys.argv[1:])