我一直想将Google身份验证添加到我的Xamarin.Forms应用程序中,而Xamarin.Android部分正在制造麻烦。基于YouTube教程,我包含了所需的库并包含了必要的代码(参见下面)。
对于API,我使用和Google平台(需要获取API密钥)。当将我的应用程序安装到一个设备上时,无论是物理的还是模拟的,签名过程在接下来的3-5天内都能正常工作。我可以选择一个谷歌帐户和认证。然而,之后,登录屏幕将被卡在加载中。
有时,不经常,API会返回。
状态{statusCode=INTERNAL_ERROR,resolution=null} (请阅读下面我尝试过的内容)
用于此过程的库:
使用Android.Gms.Auth.Api;使用Android.Gms.Auth.Api.SignIn;使用Android.Gms.Common;使用Android.Gms.Common.Apis;使用Android.OS;
预期行为:
像往常一样,Google登录弹出并自动登录用户(如果他们过去已经选择了一个帐户),或者显示可用的帐户并继续验证用户的身份。这就是它的样子,对不起德国人 到底出了什么事:
登录视图确实会弹出,尽管它只是继续加载。只有重新安装整个应用程序才能帮助修复它。
我尝试过的:
如上文所述,我有时确实
状态{statusCode=INTERNAL_ERROR,resolution=null}
返回。我已经研究过这个错误,并发现我的安卓应用程序的SHA1密钥无效或不存在可能是原因。我检查了Firebase,我的Debug和都增加了。
什么是可提及的:
GoogleManager.cs
public class GoogleManager : Java.Lang.Object, IGoogleManager, GoogleApiClient.IConnectionCallbacks, GoogleApiClient.IOnConnectionFailedListener
{
public Action<GoogleUser, string> _onLoginComplete;
public static GoogleApiClient _googleApiClient { get; set; }
public static GoogleManager Instance { get; private set; }
Context _context;
public GoogleManager()
{
_context = global::Android.App.Application.Context;
Instance = this;
}
public void Login(Action<GoogleUser, string> onLoginComplete)
{
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn).RequestIdToken("my_stuff.apps.googleusercontent.com")
.RequestEmail()
.Build();
_googleApiClient = new GoogleApiClient.Builder((_context).ApplicationContext)
.AddConnectionCallbacks(this)
.AddOnConnectionFailedListener(this)
.AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
.AddScope(new Scope(Scopes.Profile))
.Build();
_onLoginComplete = onLoginComplete;
Intent signInIntent = Auth.GoogleSignInApi.GetSignInIntent(_googleApiClient);
((MainActivity)Forms.Context).StartActivityForResult(signInIntent, 1);
_googleApiClient.Connect();
}
public void Logout()
{
var gsoBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn).RequestEmail();
GoogleSignIn.GetClient(_context, gsoBuilder.Build())?.SignOut();
_googleApiClient.Disconnect();
}
public void OnAuthCompleted(GoogleSignInResult result)
{
if (result.IsSuccess)
{
GoogleSignInAccount accountt = result.SignInAccount;
_onLoginComplete?.Invoke(new GoogleUser()
{
Name = accountt.GivenName,
Email = accountt.Email,
ID = accountt.Id,
IdToken = accountt.IdToken,
}, string.Empty);
}
else
{
_onLoginComplete?.Invoke(null, AppResources.error_this_didnt_work_text);
}
}
public void OnConnected(Bundle connectionHint)
{
}
public void OnConnectionSuspended(int cause)
{
_onLoginComplete?.Invoke(null, "Canceled!");
}
public void OnConnectionFailed(ConnectionResult result)
{
_onLoginComplete?.Invoke(null, result.ErrorMessage);
}OnActivityResult来自MainActivity.cs
protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == 1)
{
GoogleSignInResult result = Auth.GoogleSignInApi.GetSignInResultFromIntent(data);
Console.WriteLine("LOGIN RESULT" + result.Status + result.SignInAccount);
GoogleManager.Instance.OnAuthCompleted(result);
}
}希望有人能找到答案。如果你有什么需要知道的,请告诉我
发布于 2021-07-13 02:50:45
如果您得到错误代码8 (INTERNAL_ERROR),请再次检查您的应用程序注册在开发控制台。请注意,每个注册的Android客户端都通过(包名,Android签名证书SHA-1)对进行唯一标识。如果您的调试和生产环境有多个包名/签名证书,请确保注册它们的每一对。核实:
New Credentials->OAuth2 Client ID,选择Android并输入您的包名/签名证书指纹。要获得您的签名密钥证书,SHA-1:
标准调试密钥
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android其他(自定义)密钥
keytool -list -v -keystore $YOUR_KEYSTORE_LOCATIONhttps://stackoverflow.com/questions/68136115
复制相似问题