Xamarin.Forms Google API使用身份提供者对用户进行身份验证 [英] Xamarin.Forms Google API Authenticating Users with an Identity Provider

查看:73
本文介绍了Xamarin.Forms Google API使用身份提供者对用户进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然习惯于Xamarin.Forms,并且我处于非常基础的水平.我针对该问题浏览了许多文章,但最终无法解决.所以...

I am still getting used to Xamarin.Forms and I am on very basic level. I went through many articles for my issue, but to the end couldn't resolve it. So...

当前,我正在尝试在使用Droid和iOS(无WP)的Xamarin.Forms应用程序中添加Google身份验证. 到目前为止,我正在此处进行操作.我正在使用Xamarin.Auth对Google进行身份验证.

Currently I am trying to add Google authentication inside my Xamarin.Forms application, which use Droid and iOS (no WP). So far I am following guide from here. I am using Xamarin.Auth to authenticate to Google.

这是我源代码中的一部分.

Here is some part from my source code.



    private async void GoogleSheetsButton_Tapped()
    {
        string clientId = null;
        string redirectUri = null;

        if (Device.RuntimePlatform == Device.iOS)
        {
            clientId = Constants.iOSClientId;
            redirectUri = Constants.iOSRedirectUrl;
        }
        else if (Device.RuntimePlatform == Device.Android)
        {
            clientId = Constants.AndroidClientId;
            redirectUri = Constants.AndroidRedirectUrl;
        }

        var authenticator = new OAuth2Authenticator(
            clientId,
            null,
            Constants.Scope,
            new Uri(Constants.AuthorizeUrl),
            new Uri(redirectUri),
            new Uri(Constants.AccessTokenUrl),
            null,
            true);

        authenticator.Completed += OnAuthCompleted;
        authenticator.Error += OnAuthError;

        AuthenticationState.Authenticator = authenticator;

        var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
        presenter.Login(authenticator);
    }

问题出在我的方法完成工作之后.所以在我的最后一行之后:

The problem is coming after my method complete it's work. So after my last line:



    presenter.Login(authenticator);

一切看起来都很好,并且正在调试我正在跟踪编译器退出方法而没有任何错误,但是随后我收到异常,您可以看到 没有兼容的代码正在运行 ".

everything looks alright and debugging I am following that compiler goes out of method without any errors, but then I receive exception, which you can see here. It's "No compatible code running".

以下是有关我的源代码的更多信息:

Here some more information regarding my source code:

  • 用于客户端ID和URL的常量"类的来源


    public static class Constants
    {
        public static string AppName = "....";

        // OAuth
        // For Google login, configure at https://console.developers.google.com/
        public static string iOSClientId = "6.....apps.googleusercontent.com";
        public static string AndroidClientId = "6.....apps.googleusercontent.com";

        // These values do not need changing
        public static string Scope = "https://www.googleapis.com/auth/userinfo.email";
        public static string AuthorizeUrl = "https://accounts.google.com/o/oauth2/auth";
        public static string AccessTokenUrl = "https://www.googleapis.com/oauth2/v4/token";
        public static string UserInfoUrl = "https://www.googleapis.com/oauth2/v2/userinfo";

        // Set these to reversed iOS/Android client ids, with :/oauth2redirect appended
        public static string iOSRedirectUrl = "com.googleusercontent.apps.6......h:/oauth2redirect";
        public static string AndroidRedirectUrl = "com.googleusercontent.apps.6......l:/oauth2redirect";
    }

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆