AWS Cognito:处理令牌到期时间

时间:2016-02-09 15:33:31

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

我正在尝试使用Cognito,当我认为它开始没问题时,我面临的问题是(Google)令牌在1小时后过期。

当我开始使用干净的设备时,我可以注册,使用该应用程序1小时,然后在我需要刷新数据集时,我得到并且错误地说该令牌未被授权。

有没有关于如何处理这个问题的例子?

应用程序开发人员应该做些什么? 我期待SDK在后台管理这些事情。

是否意味着我们必须在每次数据集同步之前检查credentialsProvider.getSessionCredentitalsExpiration()?

非常感谢, JM

编辑1:添加了代码

我确实有SigninActivity,但只有在根本没有凭据时才会调用它,理论上只有在用户第一次登录时才会调用。

它构建如下(删除无用位)。 所以会发生的事情是我第一次正确验证,但由于我再也没有进入此活动,可能会缺少某些东西。

但必须有一种方法可以静默刷新此令牌吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.signin);

    // Aws Credential provider
    credentialsProvider = new CognitoCachingCredentialsProvider(
            getApplicationContext(), // Context
            getString(R.string.aws_identity_pool), // Identity Pool ID
            Regions.EU_WEST_1 // Region
    );

    // Google connect
    findViewById(R.id.signin_with_google_btn).setOnClickListener(this);

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestProfile()
            .requestIdToken(getString(R.string.google_server_client_id))
            .requestId()
            .build();

    // Build a GoogleApiClient with access to the Google Sign-In API and the
    // options specified by gso.
    googleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this )
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    SignInButton signInButton = (SignInButton) findViewById(R.id.signin_with_google_btn);
    signInButton.setSize(SignInButton.SIZE_WIDE);
    signInButton.setScopes(gso.getScopeArray());
    signInButton.setColorScheme(SignInButton.COLOR_DARK);
}

@Override
public void onClick(View view) {
    this.signinWithGoogle();
}

/**
 * Triggers Google signin
 */
private void signinWithGoogle() {
    Log.v(this, "Signing in with Google...");

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GOOGLE_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        this.handleGoogleSignInResult(result);
    } else {
        // Other stuff
    }
}


/**
 * Handle Google sign in result
 */
private void handleGoogleSignInResult(GoogleSignInResult result) {

    if (result.isSuccess()) {

        Log.v(this, "Successfully logged in with Google...");

        GoogleSignInAccount acct = result.getSignInAccount();
        Log.v(this, "Signed in as %s / %s (token %s)", acct.getDisplayName(), acct.getEmail(), acct.getIdToken());
        Map<String, String> logins = new HashMap<>();
        logins.put("accounts.google.com", acct.getIdToken());
        Log.v(SigninActivity.this, "Google token <<<\n%s\n>>>", logins.get("accounts.google.com"));
        credentialsProvider.setLogins(logins);
    } else {
        // Signed out
        Log.w(this, "Failed to authenticate against Google #%d - %s", result.getStatus().getStatusCode(), result.getStatus().getStatusMessage());

        SimpleMessageDialog.show(SigninActivity.this,
                R.drawable.icon,
                R.string.error,
                R.string.sorry_error_signing_you_in_please_try_again,
                R.string.try_again);
    }
}

1 个答案:

答案 0 :(得分:0)

您是否已按照here记录的所有步骤进行操作? SDK会自动为您提供AWS凭据以换取有效令牌,但如果您的Google令牌已过期,则需要刷新它。