-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram.py
79 lines (55 loc) · 2.01 KB
/
instagram.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
import urllib
import config
from flask import Blueprint, redirect, url_for, current_app, session, request, jsonify
from flask_oauthlib.client import OAuth
instagram_bp = Blueprint('instagram', __name__,
template_folder='templates/instagram')
oauth = OAuth(current_app)
instagram = oauth.remote_app(
'instagram',
consumer_key=config.INSTAGRAM_KEY,
consumer_secret=config.INSTAGRAM_SECRET,
request_token_params={'scope': 'basic'},
base_url='https://api.instagram.com/v1/',
request_token_url=None,
access_token_method='POST',
access_token_url='https://api.instagram.com/oauth/access_token',
authorize_url='https://api.instagram.com/oauth/authorize/'
)
token_name = 'instagram_token'
user_data = 'user_self'
def redirect_dummy(*args, **kwargs):
return redirect(url_for('instagram.login'))
def authenticate(f):
def wrapper(*args, **kwargs):
if token_name not in session:
return redirect_dummy(*args, **kwargs)
return f(*args, **kwargs)
return wrapper
def insta_get(url, params=None):
if not params:
params = {}
token = get_github_oauth_token()[0] or ''
params_s = urllib.urlencode(params)
return instagram.get(url + '?access_token=' + token + '&' + params_s).data
# Session management views
@instagram_bp.route('/login')
def login():
return instagram.authorize(callback=url_for('instagram.authorized', _external=True))
@instagram_bp.route('/login/authorized')
def authorized():
resp = instagram.authorized_response()
if resp is None or resp.get('access_token') is None:
return 'Access denied: reason=%s error=%s resp=%s' % (
request.args['error'],
request.args['error_description'],
resp
)
session[token_name] = (resp['access_token'], '')
session[user_data] = resp['user']
me = resp['user']
print(me)
return redirect(url_for('spotify.login'))
@instagram.tokengetter
def get_github_oauth_token():
return session.get(token_name)