All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tools.email.GoogleCredentials Maven / Gradle / Ivy

There is a newer version: 0.0.3.6
Show newest version
package tools.email;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;


class GoogleCredentials {

    private static final String credentialFilePath = "/tokens/client_id.json";


    static Credential getCredentials(List SCOPES,
                                     String TOKENS_DIRECTORY_PATH,
                                     NetHttpTransport httpTransport,
                                     JsonFactory jsonFactory, String user) throws IOException {

        // Load client secrets.
        InputStream in = GoogleCredentials.class.getResourceAsStream(credentialFilePath);
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, jsonFactory, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize(user);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy