forked from sns-sdks/python-facebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_token.py
100 lines (82 loc) · 3.57 KB
/
get_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
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
93
94
95
96
97
98
99
100
"""
Utility to get your access tokens.
Refer: https://developers.facebook.com/docs/facebook-login/access-tokens/refreshing
"""
import webbrowser
import click
import requests
from requests_oauthlib import OAuth2Session
from requests_oauthlib.compliance_fixes import facebook_compliance_fix
DEFAULT_GRAPH_URL = 'https://graph.facebook.com/v4.0/'
DEFAULT_OAUTH_URL = 'https://www.facebook.com/v4.0/dialog/oauth'
DEFAULT_TOKEN_URL = 'https://graph.facebook.com/v4.0/oauth/access_token'
DEFAULT_REDIRECT_URL = 'https://localhost:5000/'
@click.group()
def cli():
pass
@cli.command(short_help='get long term access token by short token.')
def long_term_token():
app_id = click.prompt('Please input your app id', type=str)
app_secret = click.prompt('Please input your app secret', hide_input=True, type=str)
short_token = click.prompt('Please input your short lived token', type=str)
click.echo('Begin to exchange long term access token...')
try:
resp = requests.get(
url=DEFAULT_GRAPH_URL + 'oauth/access_token',
params={
'grant_type': 'fb_exchange_token',
'client_id': app_id,
'client_secret': app_secret,
'fb_exchange_token': short_token
},
timeout=5
)
if resp.status_code == 200:
data = resp.json()
if 'error' in data:
click.echo('\nOps. May you input error.\n Info: {}'.format(data['error']))
click.echo('\nYour long term token is: \n{}'.format(data.get('access_token', '')))
else:
click.echo('\nOps. Response error.\n Info: {}'.format(resp.text))
except Exception as e:
click.echo('\nOps. Error occurred.\n Info: {}'.format(e))
@cli.command(short_help='get app access token by app info.')
def app_token():
app_id = click.prompt('Please input your app id', type=str)
app_secret = click.prompt('Please input your app secret', hide_input=True, type=str)
click.echo('Begin to retrieve app access token...')
try:
resp = requests.get(
url=DEFAULT_GRAPH_URL + 'oauth/access_token',
params={
'grant_type': 'client_credentials',
'client_id': app_id,
'client_secret': app_secret,
},
timeout=5
)
if resp.status_code == 200:
data = resp.json()
if 'error' in data:
click.echo('\nOps. May you input error.\n Info: {}'.format(data['error']))
click.echo('\nYour app access token is: \n{}'.format(data.get('access_token', '')))
else:
click.echo('\nOps. Response error.\n Info: {}'.format(resp.text))
except Exception as e:
click.echo('\nOps. Error occurred.\n Info: {}'.format(e))
@cli.command(short_help='get user access token by login.')
def user_access_token():
app_id = click.prompt('Please input your app id', type=str)
app_secret = click.prompt('Please input your app secret', hide_input=True, type=str)
facebook = OAuth2Session(client_id=app_id, redirect_uri=DEFAULT_REDIRECT_URL)
facebook = facebook_compliance_fix(facebook)
authorization_url, state = facebook.authorization_url(DEFAULT_OAUTH_URL)
webbrowser.open(authorization_url)
response = click.prompt('Please input full callback url', type=str)
facebook.fetch_token(
token_url=DEFAULT_TOKEN_URL, client_secret=app_secret,
authorization_response=response
)
click.echo("Your access token is: \n{}".format(facebook.access_token))
if __name__ == '__main__':
cli()