Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <dependency>
- <groupId>com.google.api-client</groupId>
- <artifactId>google-api-client</artifactId>
- <version>1.23.0</version>
- </dependency>
- <dependency>
- <groupId>com.google.oauth-client</groupId>
- <artifactId>google-oauth-client-jetty</artifactId>
- <version>1.23.0</version>
- </dependency>
- <dependency>
- <groupId>com.google.apis</groupId>
- <artifactId>google-api-services-drive</artifactId>
- <version>v2-rev282-1.23.0</version>
- </dependency>
- 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.googleapis.javanet.GoogleNetHttpTransport;
- import com.google.api.client.http.HttpTransport;
- import com.google.api.client.json.JsonFactory;
- import com.google.api.client.json.jackson2.JacksonFactory;
- import com.google.api.client.util.store.FileDataStoreFactory;
- import com.google.api.services.drive.Drive;
- import com.google.api.services.drive.DriveScopes;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.security.GeneralSecurityException;
- import java.util.Arrays;
- import java.util.List;
- /**
- *
- * @author jalal-sordo
- */
- public class GoogleDriveServiceFactory {
- private String applicationName;
- private JsonFactory jsonFactory;
- private HttpTransport googleDriveHttpTransport;
- private List<String> googleDriveApiScopes;
- private String googleDriveClientSecretFilePath;
- private FileDataStoreFactory credentialsStoreFolder;
- public GoogleDriveServiceFactory(ApplicationParameters params) {
- try {
- this.googleDriveClientSecretFilePath = params.getGoogleDriveClientSecretFilePath();
- // = new FileInputStream(googleDriveClientSecretFilePath);
- applicationName = "FileTXTExtractor";
- jsonFactory = JacksonFactory.getDefaultInstance();
- googleDriveApiScopes = Arrays.asList(DriveScopes.DRIVE);
- googleDriveHttpTransport = GoogleNetHttpTransport.newTrustedTransport();
- java.io.File googleDriveCredentialsStore = new java.io.File(params.getGoogleDriveCredentialsFolderPath());
- credentialsStoreFolder = new FileDataStoreFactory(googleDriveCredentialsStore);
- } catch (IOException | GeneralSecurityException t) {
- System.err.println(t.getMessage());
- System.exit(1);
- }
- }
- public Credential authorize() throws IOException {
- InputStreamReader streamReader = new FileReader(new java.io.File(googleDriveClientSecretFilePath));
- GoogleClientSecrets clientSecrets
- = GoogleClientSecrets.load(jsonFactory, streamReader);
- // Build flow and trigger user authorization request.
- GoogleAuthorizationCodeFlow flow
- = new GoogleAuthorizationCodeFlow.Builder(
- googleDriveHttpTransport, jsonFactory, clientSecrets, googleDriveApiScopes)
- .setDataStoreFactory(credentialsStoreFolder)
- .setAccessType("offline")
- .build();
- Credential credential = new AuthorizationCodeInstalledApp(
- flow, new LocalServerReceiver()).authorize("user");
- return credential;
- }
- public Drive getDriveService() throws IOException {
- Credential credential = authorize();
- return new Drive.Builder(
- googleDriveHttpTransport, jsonFactory, credential)
- .setApplicationName(applicationName)
- .build();
- }
- }
- /**
- *
- * @author jalal-sordo
- */
- public class ApplicationParameters {
- private String googleDriveCredentialsFolderPath;
- private String googleDriveClientSecretFilePath;
- public String getGoogleDriveCredentialsFolderPath() {
- return googleDriveCredentialsFolderPath;
- }
- public void setGoogleDriveCredentialsFolderPath(String googleDriveCredentialsFolderPath) {
- this.googleDriveCredentialsFolderPath = googleDriveCredentialsFolderPath;
- }
- public String getGoogleDriveClientSecretFilePath() {
- return googleDriveClientSecretFilePath;
- }
- public void setGoogleDriveClientSecretFilePath(String googleDriveClientSecretFilePath) {
- this.googleDriveClientSecretFilePath = googleDriveClientSecretFilePath;
- }
- @Override
- public String toString() {
- return "ApplicationParameters{"
- + "ngoogleDriveCredentialsFolderPath = " + googleDriveCredentialsFolderPath
- + "ngoogleDriveClientSecretFilePath = " + googleDriveClientSecretFilePath
- + "n}";
- }
- }
- ApplicationParameters params = new ApplicationParameters();
- params.setGoogleDriveCredentialsFolderPath("...");//this is where you specify where you want the credentils to be stored
- params.setGoogleDriveClientSecretFilePath(clientSecret.get());//this is the path to your client_id that you download from google cloud console credentials page.
- GoogleDriveServiceFactory driveFactory = new GoogleDriveServiceFactory (params);
- File fileMetadata = new File();
- fileMetadata.setName("photo.jpg");
- java.io.File filePath = new java.io.File("files/photo.jpg");
- FileContent mediaContent = new FileContent("image/jpeg", filePath);
- File file = driveFactory.getDriveService.files().create(fileMetadata, mediaContent)
- .setFields("id")
- .execute();
- System.out.println("File ID: " + file.getId());
Add Comment
Please, Sign In to add comment