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


Java GoogleAuthorizationCodeTokenRequest.execute方法代码示例

本文整理汇总了Java中com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute方法的典型用法代码示例。如果您正苦于以下问题:Java GoogleAuthorizationCodeTokenRequest.execute方法的具体用法?Java GoogleAuthorizationCodeTokenRequest.execute怎么用?Java GoogleAuthorizationCodeTokenRequest.execute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest的用法示例。


在下文中一共展示了GoogleAuthorizationCodeTokenRequest.execute方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: authorize

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入方法依赖的package包/类
private static void authorize(DataStoreFactory storeFactory, String userId) throws Exception {
  // Depending on your application, there may be more appropriate ways of
  // performing the authorization flow (such as on a servlet), see
  // https://developers.google.com/api-client-library/java/google-api-java-client/oauth2#authorization_code_flow
  // for more information.
  GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
      new NetHttpTransport(),
      new JacksonFactory(),
      CLIENT_ID,
      CLIENT_SECRET,
      Arrays.asList(SCOPE))
      .setDataStoreFactory(storeFactory)
      // 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.dfp.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();

  // Store the credential for the user.
  authorizationFlow.createAndStoreCredential(tokenResponse, userId);
}
 
开发者ID:googleads,项目名称:googleads-java-lib,代码行数:37,代码来源:AdvancedCreateCredentialFromScratch.java

示例2: exchangeCode

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入方法依赖的package包/类
protected Credential exchangeCode(
		String authorizationCode, String redirectUri)
	throws CodeExchangeException {

	try {
		GoogleAuthorizationCodeFlow flow = getFlow();

		GoogleAuthorizationCodeTokenRequest token = flow.newTokenRequest(
			authorizationCode);

		token.setRedirectUri(redirectUri);

		GoogleTokenResponse response = token.execute();

		return flow.createAndStoreCredential(response, null);
	}
	catch (IOException e) {
		System.err.println("An error occurred: " + e);

		throw new CodeExchangeException();
	}
}
 
开发者ID:sergiogonzalez,项目名称:google-login-hook,代码行数:23,代码来源:GoogleOAuth.java

示例3: getOAuth2Credential

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入方法依赖的package包/类
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.dfp.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;
}
 
开发者ID:googleads,项目名称:googleads-java-lib,代码行数:41,代码来源:GetRefreshToken.java

示例4: getOAuth2Credential

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入方法依赖的package包/类
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.dfp.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;
}
 
开发者ID:googleads,项目名称:googleads-java-lib,代码行数:41,代码来源:GetRefreshTokenWithoutPropertiesFile.java

示例5: getOAuth2Credential

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入方法依赖的package包/类
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.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;
}
 
开发者ID:googleads,项目名称:googleads-java-lib,代码行数:41,代码来源:GetRefreshToken.java

示例6: getOAuth2Credential

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入方法依赖的package包/类
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.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;
}
 
开发者ID:googleads,项目名称:googleads-java-lib,代码行数:41,代码来源:GetRefreshTokenWithoutPropertiesFile.java

示例7: authorize

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入方法依赖的package包/类
private static void authorize(DataStoreFactory storeFactory, String userId) throws Exception {
  // Depending on your application, there may be more appropriate ways of
  // performing the authorization flow (such as on a servlet), see
  // https://developers.google.com/api-client-library/java/google-api-java-client/oauth2#authorization_code_flow
  // for more information.
  GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
      new NetHttpTransport(),
      new JacksonFactory(),
      CLIENT_ID,
      CLIENT_SECRET,
      Arrays.asList(SCOPE))
      .setDataStoreFactory(storeFactory)
      // 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();

  // Store the credential for the user.
  authorizationFlow.createAndStoreCredential(tokenResponse, userId);
}
 
开发者ID:googleads,项目名称:googleads-java-lib,代码行数:37,代码来源:AdvancedCreateCredentialFromScratch.java

示例8: getNewOAuth2Credential

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入方法依赖的package包/类
private Credential getNewOAuth2Credential() throws OAuthException {
  GoogleAuthorizationCodeFlow authorizationFlow = getAuthorizationFlow();

  String authorizeUrl =
      authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();

  System.out.println("\n**ACTION REQUIRED** Paste this url in your browser"
      + " and authenticate using your **AdWords Admin Account**: \n\n" + authorizeUrl + '\n');

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

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

    //  Create the credential.
    Credential credential =
        new GoogleCredential.Builder()
            .setClientSecrets(clientId, clientSecret)
            .setJsonFactory(new JacksonFactory())
            .setTransport(new NetHttpTransport())
            .build()
            .setFromTokenResponse(tokenResponse);
    return credential;
  } catch (IOException e) {
    throw new OAuthException("An error occured obtaining the OAuth2Credential",  e);
  }
}
 
开发者ID:googleads,项目名称:aw-reporting,代码行数:35,代码来源:InstalledOAuth2Authenticator.java

示例9: getOAuth2Credential

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:googleads,项目名称:keyword-optimizer,代码行数:51,代码来源:AdWordsApiUtil.java

示例10: getNewOAuth2Credential

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; //导入方法依赖的package包/类
/**
 * Get New Credentials from the user from the command line OAuth2 dance.
 */
private Credential getNewOAuth2Credential() throws OAuthException {
  GoogleAuthorizationCodeFlow authorizationFlow = getAuthorizationFlow();
  String authorizeUrl =
      authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();

  System.out.println("\n**ACTION REQUIRED** Paste this url in your browser"
      + " and authenticate using your **AdWords Admin Email**: \n" + authorizeUrl);

  // Wait for the authorization code.
  System.out.println("\nType the code you received on the web page here: ");
  try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String authorizationCode = reader.readLine();

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

    //  Create the credential.
    Credential credential =
        new GoogleCredential.Builder()
            .setClientSecrets(clientId, clientSecret)
            .setJsonFactory(new JacksonFactory())
            .setTransport(new NetHttpTransport())
            .build()
            .setFromTokenResponse(tokenResponse);

    // Get refresh token and prompt to save in properties file
    refreshToken = credential.getRefreshToken();
    System.out.println("\n**ACTION REQUIRED** Put the following line in your properties file to"
        + " avoid OAuth authentication next time.");
    System.out.printf("refreshToken=%s\n\n", refreshToken);

    System.out.println("Then press enter to continue...");
    reader.readLine();

    return credential;
  } catch (IOException e) {
    throw new OAuthException("An error occured obtaining the OAuth2Credential", e.getCause());
  }
}
 
开发者ID:googleads,项目名称:adwords-alerting,代码行数:47,代码来源:InstalledOAuth2Authenticator.java


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