本文整理汇总了Java中com.google.auth.oauth2.GoogleCredentials.refreshAccessToken方法的典型用法代码示例。如果您正苦于以下问题:Java GoogleCredentials.refreshAccessToken方法的具体用法?Java GoogleCredentials.refreshAccessToken怎么用?Java GoogleCredentials.refreshAccessToken使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.auth.oauth2.GoogleCredentials
的用法示例。
在下文中一共展示了GoogleCredentials.refreshAccessToken方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: oauth2AuthToken
import com.google.auth.oauth2.GoogleCredentials; //导入方法依赖的package包/类
/** Sends a unary rpc with raw oauth2 access token credentials. */
public void oauth2AuthToken(String jsonKey, InputStream credentialsStream, String authScope)
throws Exception {
GoogleCredentials utilCredentials =
GoogleCredentials.fromStream(credentialsStream);
utilCredentials = utilCredentials.createScoped(Arrays.<String>asList(authScope));
AccessToken accessToken = utilCredentials.refreshAccessToken();
OAuth2Credentials credentials = OAuth2Credentials.create(accessToken);
TestServiceGrpc.TestServiceBlockingStub stub = blockingStub
.withCallCredentials(MoreCallCredentials.from(credentials));
final SimpleRequest request = SimpleRequest.newBuilder()
.setFillUsername(true)
.setFillOauthScope(true)
.build();
final SimpleResponse response = stub.unaryCall(request);
assertFalse(response.getUsername().isEmpty());
assertTrue("Received username: " + response.getUsername(),
jsonKey.contains(response.getUsername()));
assertFalse(response.getOauthScope().isEmpty());
assertTrue("Received oauth scope: " + response.getOauthScope(),
authScope.contains(response.getOauthScope()));
}
示例2: testCustomFirebaseCredential
import com.google.auth.oauth2.GoogleCredentials; //导入方法依赖的package包/类
@Test
public void testCustomFirebaseCredential() throws IOException {
final Date date = new Date();
FirebaseCredential credential = new FirebaseCredential() {
@Override
public Task<GoogleOAuthAccessToken> getAccessToken() {
return Tasks.forResult(new GoogleOAuthAccessToken("token", date.getTime()));
}
};
GoogleCredentials googleCredentials = new FirebaseCredentialsAdapter(credential);
AccessToken accessToken = googleCredentials.refreshAccessToken();
Assert.assertEquals("token", accessToken.getTokenValue());
Assert.assertEquals(date, accessToken.getExpirationTime());
}
示例3: doInBackground
import com.google.auth.oauth2.GoogleCredentials; //导入方法依赖的package包/类
@Override
protected AccessToken doInBackground(Void... voids) {
final SharedPreferences prefs =
getSharedPreferences(PREFS, Context.MODE_PRIVATE);
String tokenValue = prefs.getString(PREF_ACCESS_TOKEN_VALUE, null);
long expirationTime = prefs.getLong(PREF_ACCESS_TOKEN_EXPIRATION_TIME, -1);
// Check if the current token is still valid for a while
if (tokenValue != null && expirationTime > 0) {
if (expirationTime
> System.currentTimeMillis() + ACCESS_TOKEN_EXPIRATION_TOLERANCE) {
return new AccessToken(tokenValue, new Date(expirationTime));
}
}
// ***** WARNING *****
// In this sample, we load the credential from a JSON file stored in a raw resource
// folder of this client app. You should never do this in your app. Instead, store
// the file in your server and obtain an access token from there.
// *******************
final InputStream stream = getResources().openRawResource(R.raw.credential);
try {
final GoogleCredentials credentials = GoogleCredentials.fromStream(stream)
.createScoped(SCOPE);
final AccessToken token = credentials.refreshAccessToken();
prefs.edit()
.putString(PREF_ACCESS_TOKEN_VALUE, token.getTokenValue())
.putLong(PREF_ACCESS_TOKEN_EXPIRATION_TIME,
token.getExpirationTime().getTime())
.apply();
return token;
} catch (IOException e) {
Log.e(TAG, "Failed to obtain access token.", e);
}
return null;
}