Java Code Examples for com.google.auth.oauth2.GoogleCredentials#getAccessToken()

The following examples show how to use com.google.auth.oauth2.GoogleCredentials#getAccessToken() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GoogleDriveStorageService.java    From production-ready-microservices-starter with MIT License 6 votes vote down vote up
private String getAccessToken() {

        GoogleCredentials credentials = null;
        try {
            credentials = GoogleCredentials.fromStream(new FileInputStream(googleDriveServiceAccountCredentialsFilePath));
            credentials = credentials.createScoped(GOOGLE_DRIVE_APIS_SCOPES);
            credentials.refreshIfExpired();
        } catch (IOException ex) {
            throw new IORuntimeException("Could not create GoogleCredentials.", ex);
        }

        AccessToken token = credentials.getAccessToken();
        return token.getTokenValue();
    }
 
Example 2
Source File: FirebaseAuthIT.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomTokenWithIAM() throws Exception {
  FirebaseApp masterApp = IntegrationTestUtils.ensureDefaultApp();
  GoogleCredentials credentials = ImplFirebaseTrampolines.getCredentials(masterApp);
  AccessToken token = credentials.getAccessToken();
  if (token == null) {
    token = credentials.refreshAccessToken();
  }
  FirebaseOptions options = new FirebaseOptions.Builder()
      .setCredentials(GoogleCredentials.create(token))
      .setServiceAccountId(((ServiceAccountSigner) credentials).getAccount())
      .setProjectId(IntegrationTestUtils.getProjectId())
      .build();
  FirebaseApp customApp = FirebaseApp.initializeApp(options, "tempApp");
  try {
    FirebaseAuth auth = FirebaseAuth.getInstance(customApp);
    String customToken = auth.createCustomTokenAsync("user1").get();
    String idToken = signInWithCustomToken(customToken);
    FirebaseToken decoded = auth.verifyIdTokenAsync(idToken).get();
    assertEquals("user1", decoded.getUid());
  } finally {
    customApp.delete();
  }
}
 
Example 3
Source File: TestOnlyImplFirebaseTrampolines.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
public static String getToken(FirebaseApp app, boolean forceRefresh) {
  GoogleCredentials credentials = app.getOptions().getCredentials();
  try {
    if (forceRefresh) {
      credentials.refresh();
    } else {
      credentials.getRequestMetadata();
    }

    AccessToken token = credentials.getAccessToken();
    return token.getTokenValue();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 4
Source File: GoogleIdTokenAuth.java    From styx with Apache License 2.0 4 votes vote down vote up
private static AccessToken accessToken(GoogleCredentials credentials) throws IOException {
  if (credentials.getAccessToken() == null) {
    credentials.refresh();
  }
  return credentials.getAccessToken();
}