Skip to content

Instantly share code, notes, and snippets.

@damoun
Created August 30, 2017 13:22
Show Gist options
  • Save damoun/42a5b48e3b8b1555508e879eeae1c35c to your computer and use it in GitHub Desktop.
Save damoun/42a5b48e3b8b1555508e879eeae1c35c to your computer and use it in GitHub Desktop.
Error on gzipped content
from flask import Flask, redirect, url_for, session
from flask import request, jsonify
from flask_oauthlib.client import OAuth
APP = Flask(__name__)
APP.debug = True
APP.secret_key = 'development'
OAUTH = OAuth(APP)
TWITCH = OAUTH.remote_app(
'twitch',
app_key="TWITCH",
base_url='https://api.twitch.tv/kraken/',
request_token_url=None,
access_token_method='POST',
access_token_url='https://api.twitch.tv/kraken/oauth2/token',
authorize_url='https://api.twitch.tv/kraken/oauth2/authorize',
request_token_params={'scope': ["user_read", "user_subscriptions"]},
consumer_key='REPLACE_ME',
consumer_secret='REPLACE_ME'
)
def change_twitch_header(uri, headers, body):
auth = headers.get('Authorization')
if auth:
auth = auth.replace('Bearer', 'OAuth')
headers['Authorization'] = auth
headers['Accept'] = "application/vnd.twitchtv.v5+json"
return uri, headers, body
TWITCH.pre_request = change_twitch_header
@APP.route("/")
def index():
if 'twitch_token' in session:
is_sub = TWITCH.get('users/{:d}/subscriptions/{:d}'.format(
29776980, 29776980
))
print(is_sub._resp.headers)
return jsonify(is_sub.data)
return redirect(url_for('login'))
@APP.route("/login")
def login():
return TWITCH.authorize(callback=url_for('authorized', _external=True))
@APP.route('/logout')
def logout():
session.pop('twitch_token', None)
return redirect(url_for('index'))
@APP.route('/login/authorized')
def authorized():
resp = TWITCH.authorized_response()
if resp is None:
return 'Access denied: reason=%s error=%s' % (
request.args['error_reason'],
request.args['error_description']
)
session['twitch_token'] = (resp['access_token'], '')
return redirect(url_for('index'))
@TWITCH.tokengetter
def get_twitch_oauth_token():
return session.get('twitch_token')
if __name__ == '__main__':
APP.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment