tools.email.GoogleCredentials Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testing-tools Show documentation
Show all versions of testing-tools Show documentation
Tools for supporting automation testing.
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