使用VisualStudio,我最近通过Xamarin.Google.iOS.SignIn NuGet
软件包成功实现了我的Xamarin.Forms
应用程序客户流动验证。然后,此登录用于允许应用程序通过Azure移动服务MobileServicesClient
访问我们的服务器API组件。
它取决于Google IDP API的access_token
,refresh_token
和id_token
的能力 - 所有3个都可以在AS 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之前设置GoogleSignInOptions
对象,启用IDToken
和ServerAuthCode
的检索,但如何从Google中获取refresh_
或access_token
?似乎想将此责任置于我们的Sever API上。这不是我们的iOS应用程序的工作方式。有人在使用此Nuget软件包API的应用程序中成功完成了此操作吗?Xamarin Android Google Signin API是否支持我想做的事?
是的,这是可能的,请尝试以下内容(忽略我们在哪里看到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}");
}
}