com.google.api.services.gmail.GmailScopes Java Examples

The following examples show how to use com.google.api.services.gmail.GmailScopes. 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: GmailServiceMaker.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
private GoogleAuthorizationCodeFlow buildFlow(GoogleClientSecrets clientSecrets) throws IOException {
    // if the scopes need to change, the user will need to manually delete
    // <TestProperties.TEST_GMAIL_API_FOLDER>/StoredCredential
    List<String> scopes = Arrays.asList(GmailScopes.GMAIL_READONLY, GmailScopes.GMAIL_MODIFY);
    FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(new File(TestProperties.TEST_GMAIL_API_FOLDER));
    return new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes)
            .setDataStoreFactory(dataStoreFactory)
            .setAccessType("offline")
            .build();
}
 
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);
  }
}