I'm looking to make a button login with discord. For that I use flutter_web_auth but discord shows me an error with the redirect URI.
Redirect URI is not supported by client
I set up flutter_web_auth as requested:
AndroidManifest.xml
<activity android:name="com.linusu.flutter_web_auth.CallbackActivity" >
<intent-filter android:label="flutter_web_auth">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.area" />
</intent-filter>
</activity>
function
void loginWithDiscord() async {
// App specific variables
const clientId = 'myClientId' ;
const callbackUrlScheme = 'com.area';
const redirectUri = 'com.area://home'; // OR 'com.area:/';
// Construct the url
final url = Uri.https('discord.com', '/api/oauth2/authorize', {
'response_type': 'code',
'client_id': clientId,
'redirect_uri': redirectUri,
'scope': 'identify',
});
// Present the dialog to the user
final result = await FlutterWebAuth.authenticate(
url: url.toString(), callbackUrlScheme: callbackUrlScheme);
// Extract code from resulting url
final code = Uri.parse(result).queryParameters['code'];
// Use this code to get an access token
final response = await http
.post(Uri.parse('https://discord.com/api/oauth2/authorize'), body: {
'client_id': clientId,
'redirect_uri': redirectUri,
'grant_type': 'authorization_code',
'code': code,
});
// Get the access token from the response
final accessToken = jsonDecode(response.body)['access_token'] as String;
print(accessToken);
}
Your issue is similar with this one (https://github.com/discord/discord-api-docs/issues/5106) :
Discord OAuth2 with mobile require PCKE (Proof Key for Code Exchange) : https://datatracker.ietf.org/doc/html/rfc7636
In your case, you need to set up a
code_verifier
and acode_challenge
, thecode_challenge
will be sent in the authorize request with a code challenge method.After you get the
authorization_code
, you will send a request to the token endpoint, you need to use thecode_verifier
at this moment.Example :
method to generate a
code_verifier
After generating the
code_verifier
, you need to generate thecode_challenge
from thecode_verifier
.The S256 method computes the SHA-256 hash of the input and then encodes the hash value using Base64-URL. Then for this example, the
code_challenge_method
isS256
.Now, you're ready to use the Discord OAuth2 :