當前位置: 首頁>>代碼示例>>Java>>正文


Java GoogleAuthorizationCodeFlow.loadCredential方法代碼示例

本文整理匯總了Java中com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow.loadCredential方法的典型用法代碼示例。如果您正苦於以下問題:Java GoogleAuthorizationCodeFlow.loadCredential方法的具體用法?Java GoogleAuthorizationCodeFlow.loadCredential怎麽用?Java GoogleAuthorizationCodeFlow.loadCredential使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow的用法示例。


在下文中一共展示了GoogleAuthorizationCodeFlow.loadCredential方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createDfpSession

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入方法依賴的package包/類
private static DfpSession createDfpSession(DataStoreFactory storeFactory, String userId)
    throws IOException, ValidationException, ConfigurationLoadException {

  // Create a GoogleCredential with minimal information.
  GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
      new NetHttpTransport(),
      new JacksonFactory(),
      CLIENT_ID,
      CLIENT_SECRET,
      Arrays.asList(SCOPE))
      .setDataStoreFactory(storeFactory).build();

  // Load the credential.
  Credential credential = authorizationFlow.loadCredential(userId);

  // Construct a DfpSession.
  return new DfpSession.Builder()
      .fromFile()
      .withOAuth2Credential(credential)
      .build();
}
 
開發者ID:googleads,項目名稱:googleads-java-lib,代碼行數:22,代碼來源:AdvancedCreateCredentialFromScratch.java

示例2: createAdWordsSession

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入方法依賴的package包/類
private static AdWordsSession createAdWordsSession(String userId)
    throws IOException, ValidationException, ConfigurationLoadException {
  // Create a GoogleCredential with minimal information.
  GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
      new NetHttpTransport(),
      new JacksonFactory(),
      CLIENT_ID,
      CLIENT_SECRET,
      Arrays.asList(SCOPE))
      .build();

  // Load the credential.
  Credential credential = authorizationFlow.loadCredential(userId);

  // Construct a AdWordsSession.
  return new AdWordsSession.Builder()
      .fromFile()
      .withOAuth2Credential(credential)
      .build();
}
 
開發者ID:googleads,項目名稱:googleads-java-lib,代碼行數:21,代碼來源:AdvancedCreateCredentialFromScratch.java

示例3: provideCredential

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入方法依賴的package包/類
@Provides
public Credential provideCredential(
    GoogleAuthorizationCodeFlow flow,
    @ClientScopeQualifier String clientScopeQualifier) {
  try {
    // Try to load the credentials, throw an exception if we fail.
    Credential credential = flow.loadCredential(clientScopeQualifier);
    if (credential == null) {
      throw new LoginRequiredException();
    }
    return credential;
  } catch (IOException ex) {
    throw new RuntimeException(ex);
  }
}
 
開發者ID:google,項目名稱:nomulus,代碼行數:16,代碼來源:AuthModule.java

示例4: createDriveService

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入方法依賴的package包/類
public static Drive createDriveService(Environment env) throws Exception {

        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

        FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(new File(env.dataDir, "credentials"));

        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory,
                new InputStreamReader(JdBox.class.getResourceAsStream("/client_secrets.json")));

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, jsonFactory, clientSecrets, Collections.singletonList(DriveScopes.DRIVE))
                .setDataStoreFactory(dataStoreFactory)
                .setAccessType("offline")
                .build();

        Credential credential = flow.loadCredential(env.userAlias);

        if (credential == null) {

            String redirectUri = "urn:ietf:wg:oauth:2.0:oob";

            String url = flow.newAuthorizationUrl().setRedirectUri(redirectUri).build();
            System.out.println("Please open the following URL in your browser then type the authorization code:");
            System.out.println("  " + url);
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String code = br.readLine();

            GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(redirectUri).execute();
            credential = flow.createAndStoreCredential(response, env.userAlias);
        }

        return new Drive.Builder(httpTransport, jsonFactory, credential).setApplicationName("JDBox").build();
    }
 
開發者ID:stepank,項目名稱:jdbox,代碼行數:35,代碼來源:JdBox.java

示例5: main

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
	if (args.length != 3) {
		System.out.println("UploadDirs accountname folderid folder");
		return;
	}
	String folderid = args[1];
	String account = args[0];
	String folder = args[2];
	java.io.File jdrivedir = new java.io.File(
			new java.io.File(new java.io.File(
					System.getProperty("user.home")), ".gyingpan"), account);
	jdrivedir.mkdirs();
	HttpTransport httpTransport = new NetHttpTransport();
	JsonFactory jsonFactory = new JacksonFactory();

	GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
			httpTransport, jsonFactory, GDrive.CLIENT_ID,
			GDrive.CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
			.setAccessType("offline")
			.setApprovalPrompt("auto")
			.setDataStoreFactory(
					new FileDataStoreFactory(new java.io.File(jdrivedir,
							"driveauth"))).build();
	Credential credential = flow.loadCredential(args[0]);
	Drive service = new Drive.Builder(httpTransport, jsonFactory,
			credential).setHttpRequestInitializer(
			new HttpRequestInitializer() {
				@Override
				public void initialize(HttpRequest request)
						throws IOException {
					credential.initialize(request);
					request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(
							new ExponentialBackOff()));
					request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(
							new ExponentialBackOff()));
				}
			}).build();
	HashMap<String, File> gfilelist = getGDriveFileList(service, folderid);
	uploadDir(service, gfilelist, folderid,
			new java.io.File(folder).toPath());
}
 
開發者ID:tbutter,項目名稱:gyingpan,代碼行數:42,代碼來源:UploadDir.java

示例6: get

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //導入方法依賴的package包/類
public Credential get() {
  try {
    GoogleClientSecrets clientSecrets = loadGoogleClientSecrets(jsonFactory);

    DataStore<StoredCredential> dataStore = getStoredCredentialDataStore();

    // Allow user to authorize via url.
    GoogleAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow.Builder(
            httpTransport,
            jsonFactory,
            clientSecrets,
            ImmutableList.of(
                GmailScopes.GMAIL_MODIFY,
                GmailScopes.GMAIL_READONLY))
            .setCredentialDataStore(dataStore)
            .setAccessType("offline")
            .setApprovalPrompt("auto")
            .build();

    // First, see if we have a stored credential for the user.
    Credential credential = flow.loadCredential(user.getEmailAddress());

    // If we don't, prompt them to get one.
    if (credential == null) {
      String url = flow.newAuthorizationUrl()
          .setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI)
          .build();
      System.out.println("Please open the following URL in your browser then "
          + "type the authorization code:\n" + url);

      // Read code entered by user.
      System.out.print("Code: ");
      System.out.flush();
      BufferedReader br = new BufferedReader(
          new InputStreamReader(System.in));
      String code = br.readLine();

      // Generate Credential using retrieved code.
      GoogleTokenResponse response = flow.newTokenRequest(code)
          .setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI)
          .execute();

      credential =
          flow.createAndStoreCredential(response, user.getEmailAddress());
    }

    Gmail gmail = new Gmail.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName(GmailServiceModule.APP_NAME)
        .build();

    Profile profile = gmail.users()
        .getProfile(user.getEmailAddress())
        .execute();

    System.out.println(profile.toPrettyString());
    return credential;
  } catch (IOException exception) {
    throw new RuntimeException(exception);
  }
}
 
開發者ID:google,項目名稱:mail-importer,代碼行數:62,代碼來源:Authorizer.java


注:本文中的com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow.loadCredential方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。