我已经从保存访问令牌和保存刷新令牌初始化了GoogleCredential变量。但我不知道检查它是否过期?
我知道GoogleCredential有一个函数getExpiresInSeconds。但是,如果我从保存的访问令牌和保存的刷新令牌初始化GoogleCredential,则函数getExpiresInSeconds将返回null。
这种情况 getExpiresInSeconds 不会为空
mResponse = mFlow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
mCredential = new GoogleCredential.Builder().setClientSecrets(CLIENT_ID, CLIENT_SECRET).setJsonFactory(mJsonFactory).setTransport(mHttpTransport).build();
mCredential.setFromTokenResponse(mResponse);
mCredential.getExpiresInSeconds() // will return expired value in second
但在这种情况下
String accessTokenSave = mPrefs.getString(GOOGLE_DRIVE_TOKEN, "NO KEY");
String accessRefreshTokenSave = mPrefs.getString(GOOGLE_DRIVE_REFRESH_TOKEN, "NO KEY");
mHttpTransport = new NetHttpTransport();
mJsonFactory = new JacksonFactory();
mCredential = new GoogleCredential.Builder()
.setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.setJsonFactory(mJsonFactory)
.setTransport(mHttpTransport).build();
mCredential.setAccessToken(accessTokenSave);
mCredential.setRefreshToken(accessRefreshTokenSave);
mCredential.getExpiresInSeconds() // will return null value
那么我如何检查 mCredential 案例从访问令牌保存和访问刷新令牌保存加载它
访问令牌将在一小时内过期,而刷新令牌在用户吊销之前一直有效。但是,您永远不应该依赖这些时间,而应准备好在凭据不再有效时捕获异常,并做出相应的反应。
查看文档以获取有关完整流程的说明,以及实现它的示例代码:https://developers.google.com/drive/credentials