Android 上的 AWS Cognito 身份池导致 Google 身份验证失败

mip*_*pnw 4 android amazon-web-services amazon-cognito google-signin aws-cognito

NotAuthorizedException:Token is not from a supported provider of this identity pool我打电话时得到一个Amazon.CognitoIdentity.AmazonCognitoIdentityClient.GetIdAsync()

我不明白为什么,令牌是通过身份验证获得的GoogleSignInApi,并且 AWS 身份池配置为使用用于在 Android 设备上进行身份验证的相同“Google WebApp 客户端 ID”与 Google 身份验证提供程序联合。

在此处输入图片说明

我也尝试使用 2 种不同的方式获取 Google 令牌

  • 使用的结果.RequestIdToken()GoogleSignInOptions
  • 通过调用GoogleAuthUtil.GetTokenAPI

两个令牌在检查时是不同的,两者看起来都是好的令牌,并且在给AmazonCognitoIdentityClient. 显然,用户在 Android 设备上已通过身份验证,该应用程序能够获取电子邮件、显示名称等...

var googleSignInOptions = new 
GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
   .RequestIdToken("Google WebApp Client ID")
   .RequestEmail()
   .Build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
   .EnableAutoManage(
      this, // FragmentActivity
      this) // OnConnectionFailedListener
   .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
   .Build();

mGoogleApiClient.Connect();

var result = await Auth.GoogleSignInApi.SilentSignIn(mGoogleApiClient);

// Only need one or the other, trying to figure out which
var idToken = result.SignInAccount.IdToken;
var authToken = await GetGoogleAuthTokenAsync(result.SignInAccount.Email);

var shortLivedAWScredentials = new CognitoAWSCredentials("identity-pool-id", AWSConfigs.RegionEndpoint);
var cognitoClient = new AmazonCognitoIdentityClient(shortLivedAWScredentials,AWSConfigs.RegionEndpoint);

var logins = new Dictionary<string, string>();
logins["accounts.google.com"] = idToken; // same failure if I use authToken

var request = new GetIdRequest();
request.IdentityPoolId = "identity-pool-id";
request.Logins = logins;

var result = await cognitoClient.GetIdAsync(request); // THIS THROWS Amazon.CognitoIdentity.Model.NotAuthorizedException

private async Task<string> GetGoogleAuthTokenAsync(string accountEmail)
{
   Account googleAccount = new Account(accountEmail, GoogleAuthUtil.GoogleAccountType);
   string scopes = "audience:server:client_id:" + "Google WebApp Client ID"
   var token = await Task.Run(() => { return GoogleAuthUtil.GetToken(this, googleAccount, scopes); });
   return token;
}
Run Code Online (Sandbox Code Playgroud)

备注 - 在异常之后,AWS 控制台显示 Cognito 身份池增加了 1 个未经身份验证的身份,谷歌身份的数量没有变化在此处输入图片说明

mip*_*pnw 7

数小时的搜索,我终于找到了解决方案。基本上,AWS Cognito 身份池联合 Google+ 已完全失效,但不要绝望。您需要做的是联合到 OpenID 提供者

首先,转到 AWS 控制台 > IAM > Identity Providers > Create Provider > Provider Type = OpenID Connect > Provider URL = https://accounts.google.com > Audience = "您在 Google开发者控制台”

请注意,不要将“Google Web App Client ID”用于受众群体。

其次,转到 AWS 控制台 > Cognito > 联合身份池。选择您的身份池,然后单击“编辑身份池”,然后导航到 OpenID 选项卡(不是 Google+ 选项卡,甚至不要使用它不起作用的选项卡)。您现在应该会看到一个名为“accounts.google.com”的复选框,选中它。

第三,编辑您的移动应用程序源代码,并确保在构建用于调用GoogleAuthUtil.GetTokenGoogleAuthUtil.GetToken(this, googleAccount, scopes). 那是scopes = "audience:server:client_id:" + "webClientId"

现在你的调用var result = await cognitoClient.GetIdAsync(request);应该成功了。