当前位置: 首页>>代码示例>>Java>>正文


Java GoogleCredentials.refreshAccessToken方法代码示例

本文整理汇总了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()));
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:26,代码来源:AbstractInteropTest.java

示例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());
}
 
开发者ID:firebase,项目名称:firebase-admin-java,代码行数:15,代码来源:FirebaseCredentialsTest.java

示例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;
}
 
开发者ID:hypeapps,项目名称:black-mirror,代码行数:37,代码来源:SpeechService.java


注:本文中的com.google.auth.oauth2.GoogleCredentials.refreshAccessToken方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。