【问题标题】:Google Authentication fails with AWS Cognito Identity Pool on Android在 Android 上使用 AWS Cognito 身份池进行 Google 身份验证失败
【发布时间】:2018-09-03 23:39:22
【问题描述】:

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

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

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

  • GoogleSignInOptions 上使用.RequestIdToken() 的结果
  • 通过调用GoogleAuthUtil.GetToken API

两个令牌在检查时是不同的,看起来都是好的令牌,并且在给 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;
}

注意事项 - 在异常发生后,AWS 控制台显示 Cognito 身份池增长了 1 个未经身份验证的身份,谷歌身份的数量没有变化

【问题讨论】:

    标签: android amazon-web-services google-signin amazon-cognito aws-cognito


    【解决方案1】:

    搜索了几个小时,我终于找到了解决方案。基本上,AWS Cognito 身份池与 Google+ 的联合已完全破坏,但不要绝望。您需要做的是改为联合到 OpenID 提供商

    首先,转到 AWS 控制台 > IAM > Identity Providers > Create Provider > Provider Type = OpenID Connect > Provider URL = https://accounts.google.com > Audience =“您在 Google Developer Console 中创建的适用于 Android 或 iOS 的 Google Client ID”

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

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

    第三,编辑您的移动应用源代码,并确保使用您在构建用于调用GoogleAuthUtil.GetTokenGoogleAuthUtil.GetToken(this, googleAccount, scopes) 的范围字符串时在 Google Developer Console 中生成的“Google WebApp 客户端 ID”。那是scopes = "audience:server:client_id:" + "webClientId"

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 2018-04-05
      • 2023-04-03
      • 2018-11-06
      • 2020-12-31
      • 2016-08-18
      相关资源
      最近更新 更多