Flask-Dance comes with pre-set OAuth consumer configurations for a few popular OAuth providers. Flask-Dance also works with providers that aren’t in this list: see the custom section at the bottom of the page. We also welcome pull requests to add new pre-set provider configurations to Flask-Dance!
Make a blueprint for authenticating with Github using OAuth 2. This requires a client ID and client secret from Github. You should either pass them to this constructor, or make sure that your Flask application config defines them, using the variables GITHUB_OAUTH_CLIENT_ID and GITHUB_OAUTH_CLIENT_SECRET.
Parameters: |
|
---|---|
Return type: | |
Returns: | A blueprint to attach to your Flask app. |
A LocalProxy to a requests.Session that already has the Github authentication token loaded (assuming that the user has authenticated with Github at some point in the past).
Make a blueprint for authenticating with Google using OAuth 2. This requires a client ID and client secret from Google. You should either pass them to this constructor, or make sure that your Flask application config defines them, using the variables GOOGLE_OAUTH_CLIENT_ID and GOOGLE_OAUTH_CLIENT_SECRET.
Parameters: |
|
---|---|
Return type: | |
Returns: | A blueprint to attach to your Flask app. |
A LocalProxy to a requests.Session that already has the Google authentication token loaded (assuming that the user has authenticated with Google at some point in the past).
Make a blueprint for authenticating with Twitter using OAuth 1. This requires an API key and API secret from Twitter. You should either pass them to this constructor, or make sure that your Flask application config defines them, using the variables TWITTER_OAUTH_API_KEY and TWITTER_OAUTH_API_SECRET.
Parameters: |
|
---|---|
Return type: | |
Returns: | A blueprint to attach to your Flask app. |
A LocalProxy to a requests.Session that already has the Twitter authentication token loaded (assuming that the user has authenticated with Twitter at some point in the past).
Make a blueprint for authenticating with JIRA using OAuth 1. This requires a consumer key and RSA key for the JIRA appication link. You should either pass them to this constructor, or make sure that your Flask application config defines them, using the variables JIRA_OAUTH_CONSUMER_KEY and JIRA_OAUTH_RSA_KEY.
Parameters: |
|
---|---|
Return type: | |
Returns: | A blueprint to attach to your Flask app. |
A LocalProxy to a requests.Session that already has the JIRA authentication token loaded (assuming that the user has authenticated with JIRA at some point in the past).
Flask-Dance allows you to build authentication blueprints for any OAuth provider, not just the ones listed above. For example, let’s create a blueprint for a fictional OAuth provider called oauth-example.com. We check the documentation for oauth-example.com, and discover that they’re using OAuth 2, the access token URL is https://oauth-example.com/login/access_token, and the authorization URL is https://oauth-example.com/login/authorize. We could then build the blueprint like this:
from flask import Flask
from flask_dance.consumer import OAuth2ConsumerBlueprint
app = Flask(__name__)
example_blueprint = OAuth2ConsumerBlueprint(
"oauth-example", __name__,
client_key="my-key-here",
client_secret="my-secret-here",
base_url="https://oauth-example.com",
access_token_url="https://oauth-example.com/login/access_token",
authorize_url="https://oauth-example.com/login/authorize",
)
app.register_blueprint(example_blueprint, url_prefix="/login")
Now, in your page template, you can do something like:
<a href="{{ url_for("oauth-example.login") }}">Login with OAuth Example</a>
And in your views, you can make authenticated requests using the session attribute on the blueprint:
resp = example_blueprint.session.get("/user")
assert resp.ok
print("Here's the content of my response: " + resp.content)
It all follows the same patterns as the Quickstarts. You can also read the code to see how the pre-set configurations are implemented – it’s very short.