com.google.api.client.googleapis.extensions.java6.auth.oauth2.GooglePromptReceiver Java Examples

The following examples show how to use com.google.api.client.googleapis.extensions.java6.auth.oauth2.GooglePromptReceiver. 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: StorageSample.java    From cloud-storage-docs-json-api-examples with Apache License 2.0 5 votes vote down vote up
/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
  // Load client secrets.
  GoogleClientSecrets clientSecrets = null;
  try {
    clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
        new InputStreamReader(StorageSample.class.getResourceAsStream(String.format("/%s",CLIENT_SECRET_FILENAME))));
    if (clientSecrets.getDetails().getClientId() == null ||
        clientSecrets.getDetails().getClientSecret() == null) {
        throw new Exception("client_secrets not well formed.");
    }
  } catch (Exception e) {
    System.out.println("Problem loading client_secrets.json file. Make sure it exists, you are " + 
                      "loading it with the right path, and a client ID and client secret are " +
                      "defined in it.\n" + e.getMessage());
    System.exit(1);
  }

  // Set up authorization code flow.
  // Ask for only the permissions you need. Asking for more permissions will
  // reduce the number of users who finish the process for giving you access
  // to their accounts. It will also increase the amount of effort you will
  // have to spend explaining to users what you are doing with their data.
  // Here we are listing all of the available scopes. You should remove scopes
  // that you are not actually using.
  Set<String> scopes = new HashSet<String>();
  scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
  scopes.add(StorageScopes.DEVSTORAGE_READ_ONLY);
  scopes.add(StorageScopes.DEVSTORAGE_READ_WRITE);

  GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
      httpTransport, JSON_FACTORY, clientSecrets, scopes)
      .setDataStoreFactory(dataStoreFactory)
      .build();
  // Authorize.
  VerificationCodeReceiver receiver = 
      AUTH_LOCAL_WEBSERVER ? new LocalServerReceiver() : new GooglePromptReceiver();
  return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");    
}
 
Example #2
Source File: CredentialFactory.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Initialized OAuth2 credential for the "installed application" flow; where the credential
 * typically represents an actual end user (instead of a service account), and is stored as a
 * refresh token in a local FileCredentialStore.
 *
 * @param scopes list of well-formed scopes desired in the credential
 * @param transport The HttpTransport used for authorization
 * @return credential with desired scopes, possibly obtained from loading {@code filePath}.
 * @throws IOException on IO error
 */
private Credential getCredentialFromFileCredentialStoreForInstalledApp(
    List<String> scopes, HttpTransport transport) throws IOException {
  logger.atFine().log(
      "getCredentialFromFileCredentialStoreForInstalledApp(%s, %s) from '%s'",
      scopes, transport, options.getOAuthCredentialFile());

  // Initialize client secrets.
  GoogleClientSecrets.Details details =
      new GoogleClientSecrets.Details()
          .setClientId(options.getClientId().value())
          .setClientSecret(options.getClientSecret().value());
  GoogleClientSecrets clientSecrets = new GoogleClientSecrets().setInstalled(details);

  // Set up file credential store.
  FileCredentialStore credentialStore =
      new FileCredentialStore(new File(options.getOAuthCredentialFile()), JSON_FACTORY);

  // Set up authorization code flow.
  GoogleAuthorizationCodeFlow flow =
      new GoogleAuthorizationCodeFlow.Builder(transport, JSON_FACTORY, clientSecrets, scopes)
          .setCredentialStore(credentialStore)
          .setRequestInitializer(new CredentialHttpRetryInitializer())
          .setTokenServerUrl(new GenericUrl(options.getTokenServerUrl()))
          .build();

  // Authorize access.
  return new AuthorizationCodeInstalledApp(flow, new GooglePromptReceiver()).authorize("user");
}