-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_token.py
51 lines (39 loc) · 1.16 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
import os
import sys
import requests
from flask import (
Flask,
render_template,
request,
)
ID = os.getenv('INSTAGRAM_CLIENT_ID', None)
SECRET = os.getenv('INSTAGRAM_SECRET', None)
app = Flask(__name__)
app.config.from_object(__name__)
@app.route("/")
def homepage():
code = request.args.get('code', None)
if code is None:
return render_template('homepage.html', client_id=ID)
else:
api_response = requests.post(
'https://api.instagram.com/oauth/access_token',
data={
'client_id': ID,
'client_secret': SECRET,
'code': code,
'grant_type': 'authorization_code',
'redirect_uri': 'http://localhost:4726/',
}
)
json_data = api_response.json()
return render_template(
'code.html',
token=json_data['access_token'],
user_id=json_data['user']['id'],
)
if __name__ == "__main__":
if ID is None or SECRET is None:
sys.exit('Requires INSTAGRAM_CLIENT_ID and INSTAGRAM_SECRET '
'environment variables.')
app.run(port=4726, debug=True)