singingwolfboy / flask-dance

Doing the OAuth dance with style using Flask, requests, and oauthlib.
https://pypi.python.org/pypi/Flask-Dance/
MIT License
1.01k stars 158 forks source link

Problem with Twitch API #393

Closed Zenahr closed 2 years ago

Zenahr commented 2 years ago

Using https://flask-dance.readthedocs.io/en/latest/providers.html#module-flask_dance.contrib.twitch

Current implementation:

auth_twitch = make_twitch_blueprint(
    client_id='REDACTED',
    client_secret='REDACTED',
    redirect_url='http://localhost:5000/redirect',
    scope='user_read',
    )

Twitch configuration: image

I believe the Twitch configuration to be correct, so this leaves me with flask-dance. Am I missing something?

Zenahr commented 2 years ago

I might add that when initiating the auth flow the user is met with this screen:

image

Zenahr commented 2 years ago

I've also tried these options:

auth_twitch = make_twitch_blueprint(
    client_id='xxx',
    client_secret='xxx',
    redirect_url='http://localhost:5000/redirect',
    scope='user_read'
    )

It still seems to redirect to http://localhost/redirect/ instead of http://localhost:5000/redirect/ as per the options defined above.

singingwolfboy commented 2 years ago

On Twitch, you need to set the "OAuth Redirect URL" to the value of Flask-Dance's authorized_url, which is /twitch/authorized by default. That will allow Flask-Dance to receive and save the OAuth token from Twitch, and then Flask-Dance will redirect the user to the redirect_url that you define. See the Understanding the Magic doc page.

Zenahr commented 2 years ago

@singingwolfboy Thanks for your reply. The redirect leads to http://localhost/twitch/authorized, I've added this array to the OAuth Redirect URLs on Twitch:

image

However, the problem still persists.

http://localhost/twitch/authorized?error=redirect_mismatch&error_description=Parameter+redirect_uri+does+not+match+registered+URI&state=REDACTED

Zenahr commented 2 years ago

I've tried out the same exact setup with GitHub, which worked, and Twitch, which, again, failed.

Here's the complete code:

from flask import Flask, redirect, url_for
from flask_dance.contrib.twitch import make_twitch_blueprint, twitch
import os

app = Flask(__name__)
app.secret_key = "xxx"  # Replace this with your own secret!
blueprint = make_twitch_blueprint(
    client_id="xxx",
    client_secret="xxx",
)

app.register_blueprint(blueprint, url_prefix="/login")

@app.route("/")
def index():
    if not twitch.authorized:
        return redirect(url_for("twitch.login"))
    resp = twitch.get("/user")
    assert resp.ok
    return "You are @{login} on twitch".format(login=resp.json()["login"])

if __name__ == "__main__":
    app.run(debug=True, ssl_context='adhoc')

Redirect URL is set to https://127.0.0.1:5000/login/twitch/authorized on Twitch.com (for GitHub I had used https://127.0.0.1:5000/login/github/authorized with success)

The error is still https://127.0.0.1:5000/login/twitch/authorized/?error=redirect_mismatch&error_description=Parameter+redirect_uri+does+not+match+registered+URI&state=xxx

I cannot tell what the problem here is as I've used the same, exact setup successfully with GitHub as a provider but have it fail with Twitch @singingwolfboy

Zenahr commented 2 years ago

I solved this problem by waiting a couple minutes for Twitch to take effect over the new changes I made.