【发布时间】:2016-05-19 16:01:32
【问题描述】:
我正在尝试 Cognito,当我认为它开始没问题时,我遇到了(Google)令牌在 1 小时后过期的问题。
当我开始使用干净的设备时,我可以注册,使用该应用程序 1 小时,然后当我需要刷新数据集时,我收到并错误提示令牌未经授权。
有没有例子说明如何处理这个问题?
这真的是应用程序开发人员应该做的事情吗? 我期待 SDK 在后台管理这些事情。
是不是说每次数据同步前都要检查credentialsProvider.getSessionCredentitalsExpiration()?
非常感谢, 杰米
编辑 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);
}
}
【问题讨论】:
标签: android amazon-web-services google-signin amazon-cognito