dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.08k stars 1.73k forks source link

WebAuthenticator Redirect URI Issue Using OAuth 2.0 Client IDs #25323

Open TGCRidham opened 3 days ago

TGCRidham commented 3 days ago

I'm trying to implement Google login in MAUI, and I manage it using WebAuthenticator. I successfully redirect to the web, and I can select my Google account; however, after logging in, I need to reopen my mobile app again, which is not working. Please check my code:

private async void OnGoogleSignInTapped(object sender, EventArgs e)
{
    try
    {
        var clientId = "blablabla";
        var redirectUri = "";
        var authUrl = $"https://accounts.google.com/o/oauth2/v2/auth?client_id={clientId}&response_type=code&scope=openid%20email%20profile&redirect_uri={redirectUri}";

        var authResult = await WebAuthenticator.AuthenticateAsync(
            new Uri(authUrl),
            new Uri(redirectUri));

        if (authResult.Properties.TryGetValue("code", out var code))
        {
            await ExchangeAuthorizationCodeForTokensAsync(code, clientId, redirectUri);
        }
        else
        {
            await DisplayAlert("Error", "Authorization code not received", "OK");
        }
    }
    catch (Exception ex)
    {
        await DisplayAlert("Google Sign-In", $"Error: {ex.Message}", "OK");
    }
}
ysbakker commented 10 hours ago

If you want to redirect back to your app, you can use a custom URI scheme or deep link in your redirectUri. You just have to make sure your app is listening to that URI scheme. Microsoft has documentation for this: