【问题标题】:Android, AWS Cognito Google OAuth authentication goes unauthenticated using CognitoCachingCredentialsProviderAndroid、AWS Cognito Google OAuth 身份验证未使用 CognitoCachingCredentialsProvider 进行身份验证
【发布时间】:2016-11-01 07:44:36
【问题描述】:

我使用以下代码在我的 Android 应用中使用 Google OAuth2 登录:

在我的登录活动 onCreate 方法中,我有:

    googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestScopes(new Scope(Scopes.PLUS_LOGIN))
            //requestIdToken takes the google cloud console's project's WEB (not Android) openID client ID.
            .requestIdToken("my client id")
            .requestEmail()
            .build();

    SignInButton signInButton = (SignInButton) oAuth.findViewById(R.id.sign_in_button);
    signInButton.setSize(SignInButton.SIZE_WIDE);
    signInButton.setScopes(googleSignInOptions.getScopeArray());

    googleApiClient = new GoogleApiClient.Builder(oAuth)
            .enableAutoManage(oAuth, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    Log.d("GoogleOAuth", "Connection failed\n" + connectionResult.getErrorMessage() );
                    Toast.makeText(context, "Failed to connect to Google", Toast.LENGTH_LONG).show();
                }
            })
            .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
            .build();

我为登录按钮注册了一个 onClick 监听器,它调用:

    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
    activity.startActivityForResult(signInIntent, GOOGLE_SIGN_IN);

我得到活动结果:

public void onActivityResult(OAuth oAuth, Context context, int requestCode, int resultCode, Intent data) {
    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == GOOGLE_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            GoogleSignInAccount account = result.getSignInAccount();
            String idToken = account.getIdToken(); //this has a value

            Map<String, String> logins = new HashMap<String, String>();
            logins.put("accounts.google.com", token);
            AWSCommunicator.setLogins(oAuth, logins, account);
            credentialsProvider.setLogins(logins);
        }
        else {
            Toast.makeText(context, "failed to login" + result.getStatus().toString(), Toast.LENGTH_LONG).show();
        }
    } else {
        Log.d("GoogleAuth", "Bad requestCode: " + requestCode);
    }
}

现在一切运行良好,似乎一切正常。这是我看到的一些日志。

D/CognitoCachingCredentialsProvider: Identity id is changed
D/CognitoCachingCredentialsProvider: Saving identity id to SharedPreferences
D/CognitoCachingCredentialsProvider: Clearing credentials from SharedPreferences
D/CognitoCachingCredentialsProvider: Saving credentials to SharedPreferences
D/CognitoCachingCredentialsProvider: Saving identity id to SharedPreferences

一切都很好。我已通过身份验证,我可以对我的 API 网关端点进行经过身份验证的调用(使用 AWS Api Gateway 自动生成的 SDK)。

现在一个小时后,我尝试拨打电话,但该令牌已过期。如何获取新令牌?然后如何刷新 Cognito?

【问题讨论】:

    标签: android amazon-web-services authentication oauth


    【解决方案1】:

    您应该能够从 Google 提供的刷新令牌中获取新的 ID 令牌。该文档可能会有所帮助。 https://developers.google.com/api-client-library/java/google-api-java-client/oauth2

    要刷新 Cognito 凭据,您只需使用来自 IdP 的最新令牌(在本例中为 Google Oauth)使 credentialsProvider 保持最新。凭据过期后,如果 CognitoCachingCredentialsProvider 拥有最新的令牌,它将自动刷新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 2018-01-29
      • 2023-04-03
      • 2017-03-11
      • 2011-10-20
      相关资源
      最近更新 更多