Skip to content

Instantly share code, notes, and snippets.

import requests
from requests_oauthlib import OAuth1
from flask import Flask, request, redirect, session
from urlparse import parse_qs
import os
app = Flask(__name__)
# This information is obtained upon registration of a new client on twitter
key = u"<your client key>"
@ib-lundgren
ib-lundgren / github_flask_oauth2.py
Created September 10, 2013 10:53
Example of how to use Flask with requests-oauthlib to fetch a GitHub user profile using an OAuth 2 token.
from requests_oauthlib import OAuth2Session
from flask import Flask, request, redirect, session, url_for
from flask.json import jsonify
import os
app = Flask(__name__)
# This information is obtained upon registration of a new GitHub
client_id = "<your client key>"
@ib-lundgren
ib-lundgren / gmail.py
Created September 25, 2013 13:25
How to fetch emails from GMail using an OAuth 2 Bearer token and GMails SASL XOAuth2 mechanism.
"""Fetching the latest GMail email using OAuth 2 and IMAP.
Requires requests-oauthlib, which is available on pypi.
Includes a basic SASL XOAUTH2 authentication method for imaplib.
"""
# Credentials you get from registering a new web application in Google API Console
client_id = 'your-id.apps.googleusercontent.com'
client_secret = 'your secret'
redirect_uri = 'your callback uri'
@ib-lundgren
ib-lundgren / refresh_example.py
Last active May 7, 2022 06:15
Work in progress example of how to do oauth 2 token refresh in a web application. Requires python modules oauthlib master - pip install -e git+https://github.com/idan/oauthlib#egg=oauthlib requests-oauthlib latest from pip (don't think master is needed) flask & requests
from json import dumps
from flask import Flask, request, redirect, session, url_for
from flask.json import jsonify
from requests_oauthlib import OAuth2Session
app = Flask(__name__)
# Ignore this, only used as basic doc string template creator.
def docstring_templator(f):
@ib-lundgren
ib-lundgren / oauth2_webapp_demo.py
Created July 9, 2012 21:02
OAuth2 webapp with OAuthLib
# Non magic version, client is only used to append tokens
# All other actions are explicit
import requests
from requests.auth import AuthBase
from oauthlib.oauth2.draft25 import WebApplicationClient
from oauthlib.common import urldecode
# Very basic auth, only used to append tokens to requests
@ib-lundgren
ib-lundgren / gist:3070603
Created July 8, 2012 11:33
Bit of magic OAuth2
# The slightly magic version, extra requests are made to obtain token
# and users must fetch values from client when saving them to a db
import requests
from requests.auth import AuthBase
from oauthlib.oauth2.draft25 import WebApplicationClient
from oauthlib.common import urldecode, generate_token
class OAuth2WebApp(WebApplicationClient, AuthBase):
@ib-lundgren
ib-lundgren / gist:3070602
Created July 8, 2012 11:33
Explicit no magic OAuth2
# Non magic version, client is only used to append tokens
# All other actions are explicit
import requests
from requests.auth import AuthBase
from oauthlib.oauth2.draft25 import WebApplicationClient
from oauthlib.common import urldecode
class OAuth2WebApp(WebApplicationClient, AuthBase):
@ib-lundgren
ib-lundgren / gist:2584789
Created May 3, 2012 09:52
OAuth1 using requests, OAuthLib and RSA signatures
import requests
from requests.auth import OAuth1
from oauthlib.oauth1.rfc5849 import SIGNATURE_RSA
client_key = u'...'
# You need to register your key with the OAuth provider first,
# in this case Google at https://accounts.google.com/ManageDomains
key = open("your_rsa_key.pem").read()