Java Code Examples for com.google.api.client.googleapis.auth.oauth2.GoogleCredential#setFromTokenResponse()

The following examples show how to use com.google.api.client.googleapis.auth.oauth2.GoogleCredential#setFromTokenResponse() . 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: GetRefreshToken.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
private static Credential getOAuth2Credential(GoogleClientSecrets clientSecrets)
    throws Exception {
  GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
      new NetHttpTransport(),
      new JacksonFactory(),
      clientSecrets,
      SCOPES)
      // Set the access type to offline so that the token can be refreshed.
      // By default, the library will automatically refresh tokens when it
      // can, but this can be turned off by setting
      // api.admanager.refreshOAuth2Token=false in your ads.properties file.
      .setAccessType("offline").build();

  String authorizeUrl =
      authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();
  System.out.printf("Paste this url in your browser:%n%s%n", authorizeUrl);

  // Wait for the authorization code.
  System.out.println("Type the code you received here: ");
  @SuppressWarnings("DefaultCharset") // Reading from stdin, so default charset is appropriate.
  String authorizationCode = new BufferedReader(new InputStreamReader(System.in)).readLine();

  // Authorize the OAuth2 token.
  GoogleAuthorizationCodeTokenRequest tokenRequest =
      authorizationFlow.newTokenRequest(authorizationCode);
  tokenRequest.setRedirectUri(CALLBACK_URL);
  GoogleTokenResponse tokenResponse = tokenRequest.execute();

  // Create the OAuth2 credential.
  GoogleCredential credential = new GoogleCredential.Builder()
      .setTransport(new NetHttpTransport())
      .setJsonFactory(new JacksonFactory())
      .setClientSecrets(clientSecrets)
      .build();

  // Set authorized credentials.
  credential.setFromTokenResponse(tokenResponse);

  return credential;
}
 
Example 2
Source File: GetRefreshTokenWithoutPropertiesFile.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
private static Credential getOAuth2Credential(GoogleClientSecrets clientSecrets)
    throws Exception {
  GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
      new NetHttpTransport(),
      new JacksonFactory(),
      clientSecrets,
      Arrays.asList(SCOPE))
      // Set the access type to offline so that the token can be refreshed.
      // By default, the library will automatically refresh tokens when it
      // can, but this can be turned off by setting
      // api.admanager.refreshOAuth2Token=false in your ads.properties file.
      .setAccessType("offline").build();

  String authorizeUrl =
      authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();
  System.out.printf("Paste this url in your browser:%n%s%n", authorizeUrl);

  // Wait for the authorization code.
  System.out.println("Type the code you received here: ");
  @SuppressWarnings("DefaultCharset") // Reading from stdin, so default charset is appropriate.
  String authorizationCode = new BufferedReader(new InputStreamReader(System.in)).readLine();

  // Authorize the OAuth2 token.
  GoogleAuthorizationCodeTokenRequest tokenRequest =
      authorizationFlow.newTokenRequest(authorizationCode);
  tokenRequest.setRedirectUri(CALLBACK_URL);
  GoogleTokenResponse tokenResponse = tokenRequest.execute();

  // Create the OAuth2 credential.
  GoogleCredential credential = new GoogleCredential.Builder()
      .setTransport(new NetHttpTransport())
      .setJsonFactory(new JacksonFactory())
      .setClientSecrets(clientSecrets)
      .build();

  // Set authorized credentials.
  credential.setFromTokenResponse(tokenResponse);

  return credential;
}
 
Example 3
Source File: GetRefreshToken.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
private static Credential getOAuth2Credential(GoogleClientSecrets clientSecrets)
    throws IOException {
  GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
      new NetHttpTransport(),
      new JacksonFactory(),
      clientSecrets,
      SCOPES)
      // Set the access type to offline so that the token can be refreshed.
      // By default, the library will automatically refresh tokens when it
      // can, but this can be turned off by setting
      // api.adwords.refreshOAuth2Token=false in your ads.properties file.
      .setAccessType("offline").build();

  String authorizeUrl =
      authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();
  System.out.printf("Paste this url in your browser:%n%s%n", authorizeUrl);

  // Wait for the authorization code.
  System.out.println("Type the code you received here: ");
  @SuppressWarnings("DefaultCharset") // Reading from stdin, so default charset is appropriate.
  String authorizationCode = new BufferedReader(new InputStreamReader(System.in)).readLine();

  // Authorize the OAuth2 token.
  GoogleAuthorizationCodeTokenRequest tokenRequest =
      authorizationFlow.newTokenRequest(authorizationCode);
  tokenRequest.setRedirectUri(CALLBACK_URL);
  GoogleTokenResponse tokenResponse = tokenRequest.execute();

  // Create the OAuth2 credential.
  GoogleCredential credential = new GoogleCredential.Builder()
      .setTransport(new NetHttpTransport())
      .setJsonFactory(new JacksonFactory())
      .setClientSecrets(clientSecrets)
      .build();

  // Set authorized credentials.
  credential.setFromTokenResponse(tokenResponse);

  return credential;
}
 
Example 4
Source File: GetRefreshTokenWithoutPropertiesFile.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
private static Credential getOAuth2Credential(GoogleClientSecrets clientSecrets)
    throws IOException {
  GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
      new NetHttpTransport(),
      new JacksonFactory(),
      clientSecrets,
      Arrays.asList(SCOPE))
      // Set the access type to offline so that the token can be refreshed.
      // By default, the library will automatically refresh tokens when it
      // can, but this can be turned off by setting
      // api.adwords.refreshOAuth2Token=false in your ads.properties file.
      .setAccessType("offline").build();

  String authorizeUrl =
      authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();
  System.out.printf("Paste this url in your browser:%n%s%n", authorizeUrl);

  // Wait for the authorization code.
  System.out.println("Type the code you received here: ");
  @SuppressWarnings("DefaultCharset") // Reading from stdin, so default charset is appropriate.
  String authorizationCode = new BufferedReader(new InputStreamReader(System.in)).readLine();

  // Authorize the OAuth2 token.
  GoogleAuthorizationCodeTokenRequest tokenRequest =
      authorizationFlow.newTokenRequest(authorizationCode);
  tokenRequest.setRedirectUri(CALLBACK_URL);
  GoogleTokenResponse tokenResponse = tokenRequest.execute();

  // Create the OAuth2 credential.
  GoogleCredential credential = new GoogleCredential.Builder()
      .setTransport(new NetHttpTransport())
      .setJsonFactory(new JacksonFactory())
      .setClientSecrets(clientSecrets)
      .build();

  // Set authorized credentials.
  credential.setFromTokenResponse(tokenResponse);

  return credential;
}
 
Example 5
Source File: AdWordsApiUtil.java    From keyword-optimizer with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new oauth2 credential based on the given client secrets.
 * 
 * @param clientSecrets the client secrets (see developer console)
 * @return the newly created credential
 * @throws IOException in case of an error reading the configuration files
 */
private static Credential getOAuth2Credential(GoogleClientSecrets clientSecrets)
    throws IOException {
  GoogleAuthorizationCodeFlow authorizationFlow =
      new GoogleAuthorizationCodeFlow
          .Builder(
              new NetHttpTransport(),
              new JacksonFactory(),
              clientSecrets,
              Lists.newArrayList(SCOPE))
          // Set the access type to offline so that the token can be refreshed. By default, the
          // library will automatically refresh tokens when it can, but this can be turned off by 
          // setting api.adwords.refreshOAuth2Token=false in your ads.properties file.
          .setAccessType("offline")
          .build();

  String authorizeUrl =
      authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();
  System.out.println("Paste this url in your browser: \n" + authorizeUrl + '\n');

  // Wait for the authorization code.
  System.out.println("Type the code you received here: ");
  String authorizationCode =
      new BufferedReader(new InputStreamReader(System.in, UTF_8)).readLine();

  // Authorize the OAuth2 token.
  GoogleAuthorizationCodeTokenRequest tokenRequest =
      authorizationFlow.newTokenRequest(authorizationCode);
  tokenRequest.setRedirectUri(CALLBACK_URL);
  GoogleTokenResponse tokenResponse = tokenRequest.execute();

  // Create the OAuth2 credential.
  GoogleCredential credential =
      new GoogleCredential.Builder()
          .setTransport(new NetHttpTransport())
          .setJsonFactory(new JacksonFactory())
          .setClientSecrets(clientSecrets)
          .build();

  // Set authorized credentials.
  credential.setFromTokenResponse(tokenResponse);

  return credential;
}