Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin谷歌Android登录-刷新和访问令牌_Android_Xamarin_Token_Google Signin - Fatal编程技术网

Xamarin谷歌Android登录-刷新和访问令牌

Xamarin谷歌Android登录-刷新和访问令牌,android,xamarin,token,google-signin,Android,Xamarin,Token,Google Signin,使用VisualStudio,我最近通过Xamarin.Google.iOS.SignIn NuGet包成功地为我的Xamarin.Forms应用程序实现了客户端流身份验证。然后,此登录用于允许应用程序通过Azure Mobile ServicesMobileServicesClient访问我们的服务器API组件 它取决于能否将Google IDP API中的访问令牌、刷新令牌、和id令牌传递到Azure,这三种令牌都可以作为登录到SignIn.SharedInstance.CurrentUse

使用VisualStudio,我最近通过
Xamarin.Google.iOS.SignIn NuGet
包成功地为我的
Xamarin.Forms
应用程序实现了客户端流身份验证。然后,此登录用于允许应用程序通过Azure Mobile Services
MobileServicesClient
访问我们的服务器API组件

它取决于能否将Google IDP API中的
访问令牌
刷新令牌
、和
id令牌
传递到Azure,这三种令牌都可以作为
登录到SignIn.SharedInstance.CurrentUser.Authentication.AccessToken
后很好地使用,
SignIn.SharedInstance.CurrentUser.Authentication.RefreshToken
SignIn.SharedInstance.CurrentUser.Authentication.IdToken

我现在正试图使用
Xamarin.GooglePlayServices.Auth NuGet
包在我们的
Xamarin.Forms
应用程序的Android版本中实现类似的行为。 然而,尽管我认为我已经具备了使用Google成功进行身份验证的基础,但我看不出如何从结果中检索所需的令牌


google文档讨论了在构建APIClient之前通过设置
GoogleSigningOptions
对象来检索
IDToken
ServerAuthCode
,但没有讨论如何从google获取
refresh
access\u token
?它似乎想把这个责任推到我们的服务器API上。这不是我们的iOS应用程序的工作方式。有人使用此NuGet package API在应用程序中成功完成此操作吗?Xamarin Android Google SignIn API是否支持我想做的事情?

是的,这是可能的,尝试以下方法(不管我们在哪里看到App.PostLoginAction-这是特定于我的应用程序的,当然你可以采取类似的方法,并在应用程序级别执行操作,就像我为获取结果所做的那样)


是的,这是可能的,请尝试以下操作(不管我们在哪里看到App.PostLoginAction-这是我的应用程序特有的,尽管您当然可以采取类似的方法并在应用程序级别执行操作,就像我为获取结果所做的那样)

public class GoogleLoginService : Java.Lang.Object, GoogleApiClient.IConnectionCallbacks, GoogleApiClient.IOnConnectionFailedListener
{
    public static GoogleApiClient GoogleApiClient { get; set; }
    public static GoogleLoginService Instance { get; private set; }

    public GoogleLoginService()
    {
        Instance = this;
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
            .RequestIdToken("YourAppsWebClientId") //note this is the WEB client id, NOT the android app client id
            .RequestEmail()
            .Build();

        GoogleApiClient = new GoogleApiClient.Builder(CrossCurrentActivity.Current.Activity) //note you need to install the plugin king's (James Montemagno) Plugin.CurrentActivity nuget for this
            .AddConnectionCallbacks(this)
            .AddOnConnectionFailedListener(this)
            .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .AddScope(new Scope(Scopes.Email))
            .AddScope(new Scope(Scopes.Profile))
            .Build();
    }

    public void Login()
    {
        Intent signInIntent = Auth.GoogleSignInApi.GetSignInIntent(GoogleApiClient);
        CrossCurrentActivity.Current.Activity.StartActivityForResult(signInIntent, 1);
        GoogleApiClient.Connect();
    }

    public void Logout()
    {
        GoogleApiClient.Disconnect();
    }

    public void OnAuthCompleted(GoogleSignInResult result)
    {
        if (result.IsSuccess)
        {               
            Task.Factory.StartNew(()=> {
                //note result.SignInAccount.IdToken is NOT the access token, so we need this:
                var accessToken = GoogleAuthUtil.GetToken(Android.App.Application.Context, result.SignInAccount.Account, $"oauth2:{Scopes.Email} {Scopes.Profile}");
                App.PostLoginAction(SocialProvider.Google, accessToken , "Success!");
            });

            //to get user profile directly, without having to make separate call using the access token:
            GoogleSignInAccount account = result.SignInAccount; //contains stuff like name, email, pic url, gender
        }
        else
        {
            App.PostLoginAction(SocialProvider.Google, null, "An error occurred.");
        }
    }

    public void OnConnected(Bundle connectionHint)
    {

    }

    public void OnConnectionSuspended(int cause)
    {
        App.PostLoginAction(SocialProvider.Google, null, "Canceled!");
    }

    public void OnConnectionFailed(ConnectionResult result)
    {
        App.PostLoginAction(SocialProvider.Google, null, $"An error occurred: {result.ErrorMessage}");
    }
}