daily pastebin goal
60%
SHARE
TWEET

Twitchalerts OAuth

a guest Sep 20th, 2016 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python2.7
  2. import os
  3. from config import client_id, client_secret, mysql_credentials, redirect_uri
  4.  
  5. from flask import Flask, redirect, request, session
  6. from requests_oauthlib import OAuth2Session
  7.  
  8. os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
  9.  
  10. app = Flask(__name__)
  11.  
  12.  
  13. # This information is obtained upon registration of a new GitHub OAuth
  14. # application here: https://github.com/settings/applications/new
  15.  
  16.  
  17. authorization_base_url = 'https://www.twitchalerts.com/api/v1.0/authorize'
  18. token_url = 'https://www.twitchalerts.com/api/v1.0/token'
  19. scope = ["alerts.create", "donations.read", "donations.create"]
  20.  
  21.  
  22. @app.route("/twitchalerts/authorize")
  23. def authorize():
  24.     """Step 1: User Authorization.
  25.  
  26.    Redirect the user/resource owner to the OAuth provider (i.e. Github)
  27.    using an URL with a few key OAuth parameters.
  28.    """
  29.     twitchalerts = OAuth2Session(
  30.         client_id, scope='alerts.create', redirect_uri=redirect_uri)
  31.     authorization_url, state = twitchalerts.authorization_url(
  32.         authorization_base_url)
  33.     # State is used to prevent CSRF, keep this for later.
  34.     session['oauth_state'] = state
  35.     return redirect(authorization_url)
  36.  
  37.  
  38. # Step 2: User authorization, this happens on the provider.
  39.  
  40. @app.route("/twitchalerts/authorized", methods=["GET", "POST"])
  41. def authorized():
  42.     """ Step 3: Retrieving an access token.
  43.  
  44.    The user has been redirected back from the provider to your registered
  45.    callback URL. With this redirection comes an authorization code included
  46.    in the redirect URL. We will use that to obtain an access token.
  47.    """
  48.     code = request.args.get('code', '')
  49.     twitchalerts = OAuth2Session(
  50.         client_id, redirect_uri=redirect_uri)  # state=session['oauth_state']
  51.     token = twitchalerts.fetch_token(
  52.         token_url, client_secret=client_secret, code=code)
  53.     """ Here is an example of cURL snippet for obtaining an access_token
  54.    This will pass everything as Form Data
  55.    curl  \
  56.        -X POST -F 'grant_type=authorization_code' -F 'client_id=YOUR_CLIENT_ID' -F 'client_secret=YOUR_CLIENT_SECRET' -F 'redirect_uri=YOUR_REDIRECT_URI' -F  'code=YOUR_CODE' \
  57.        https://www.twitchalerts.com/api/v1.0/token
  58.    """
  59.     params = {'access_token': token['access_token'], 'limit': 100}
  60.     data = twitchalerts.get(
  61.         'https://www.twitchalerts.com/api/v1.0/donations', params=params)
  62.     print(data)
  63.     return str(token["access_token"])
  64.  
  65.  
  66. if __name__ == "__main__":
  67.     # This allows us to use a plain HTTP callback
  68.     os.environ['DEBUG'] = "1"
  69.     app.secret_key = os.urandom(24)
  70.     app.run(debug=True, port=8080)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top