【问题标题】:Xamarin.Auth2 using google authentication error redirect_uri_mismatchXamarin.Auth2 使用谷歌身份验证错误 redirect_uri_mismatch
【发布时间】:2018-04-11 22:43:09
【问题描述】:

大家好,我在使用 Xamarin.OAuth2 进行 Google 身份验证时遇到了奇怪的问题。登录可以进行身份​​验证,但似乎 Xamarin.Auth 收到来自谷歌的无效响应。当我在 Google Chrome https://accounts.google.com/o/oauth2/auth 中使用此 URL 进行测试时,它会将页面重定向到 mysite URL,但使用该应用程序时,我收到此错误 redirect_uri_mismatch。

对于我使用实时网站的重定向 URL。

【问题讨论】:

    标签: xamarin xamarin.android xamarin.forms xamarin-studio


    【解决方案1】:

    您是否确保提供给 Xamarin 身份验证初始化的重定向 URI 与您在 授权重定向 URI 下添加到 Google 开发人员控制台中的重定向 URI 匹配?

    例如,下面的https://mysite.domain.net/ 应该与 Google Developer Console 网站上的授权重定向 URI 字段中列出的重定向 URI 相同:

    Xamarin.Auth.OAuth2Authenticator auth = new Xamarin.Auth.OAuth2Authenticator(
        "560741372047-t00000000000v4vf0nvp0o7aqbu94.apps.googleusercontent.com",
        "sHqEPlKg0000000000iP1x",
        "openid email",
        new System.Uri("https://accounts.google.com/o/oauth2/auth"),
        new System.Uri("https://mysite.domain.net/"),
        new System.Uri("https://accounts.google.com/o/oauth2/token"));
    

    【讨论】:

    • 是的,如果不匹配,您将无法获得身份验证屏幕。
    • 登录成功,当我点击 ALLOW 时,将您重定向到确认 DENY 或 ALLOW,DisplayAlert 弹出该错误。
    【解决方案2】:

    以这种方式使用重定向网址

    public const string RedirectUrl = "com.woodenmoose.xamarin.googleauth:/oauth2redirect";
    

    [Activity(Label = "GoogleAuthInterceptor")]
    [
        IntentFilter
        (
            actions: new[] { Intent.ActionView },
            Categories = new[]
            {
                    Intent.CategoryDefault,
                    Intent.CategoryBrowsable
            },
            DataSchemes = new[]
            {
                // First part of the redirect url (Package name)
                "com.woodenmoose.xamarin.googleauth"
            },
            DataPaths = new[]
            {
                // Second part of the redirect url (Path)
                "/oauth2redirect"
            }
        )
    ]
    

    如果您感到困惑,可以参考this project。当我面临同样的问题时,它对我有用。

    【讨论】:

    • 实际上我已经解决了整个头痛问题,只需不使用 xamarin 进行项目,我现在使用的是 Android Java。所以谢谢你。
    • 没问题@Jake Talledo
    猜你喜欢
    • 2015-01-24
    • 2012-06-05
    • 2015-06-03
    • 2016-09-15
    • 2016-10-30
    • 2012-09-20
    • 2020-08-15
    • 2018-01-01
    • 1970-01-01
    相关资源
    最近更新 更多