com.google.api.client.googleapis.auth.oauth2.GoogleOAuthConstants Java Examples

The following examples show how to use com.google.api.client.googleapis.auth.oauth2.GoogleOAuthConstants. 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: OfflineCredentials.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Fills in defaults if {@code null}.
 */
private void defaultOptionals() {
  if (this.httpTransport == null) {
    this.httpTransport = DEFAULT_HTTP_TRANSPORT;
  }

  if (this.tokenServerUrl == null) {
    this.tokenServerUrl = GoogleOAuthConstants.TOKEN_SERVER_URL;
  }
  
  if (this.scopes == null) {
    this.scopes = Lists.newArrayList(this.oAuthConfig.getScope());
  }
}
 
Example #2
Source File: Authorizer.java    From mail-importer with Apache License 2.0 4 votes vote down vote up
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);
  }
}
 
Example #3
Source File: GooglePromptReceiver.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public String getRedirectUri() throws IOException {
  return GoogleOAuthConstants.OOB_REDIRECT_URI;
}
 
Example #4
Source File: MockTokenServerTransport.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
public MockTokenServerTransport() {
  this(GoogleOAuthConstants.TOKEN_SERVER_URL);
}