- Saved searches
- Use saved searches to filter your results more quickly
- softcom-lab/ibox
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Java quickstart
- Objectives
- Prerequisites
- Set up your environment
- Enable the API
- Configure the OAuth consent screen
- Authorize credentials for a desktop application
- Prepare the workspace
- Set up the sample
- Run the sample
- Next steps
- Saved searches
- Use saved searches to filter your results more quickly
- mmiele/google-drive-client-java
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A simple version of Google Drive client in Java.
softcom-lab/ibox
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
A simple version of the Google Drive client written in Java.
- Install the Eclipse 4+ ( http://www.eclipse.org/ ), and make sure Maven plugin is available.
- (Optional) The Maven plugin is not availble in your Eclipse, install it using the update site ( http://www.eclipse.org/m2e/download/ ).
- Install the latest Maven command-line tool ( http://maven.apache.org/download.cgi )
Importing the Project into Eclipse
- File->Import->Maven->Existing Maven Projects
- Select the directory containing the pom.xml file
- Finish
Building the Project for the First Time
Configure Google Drive API Credential
- Please follow the instructions to setup the Google account credential at: https://developers.google.com/drive/v3/web/quickstart/java#step_1_turn_on_the_api_name
- When creating the Client ID, choose: «OAuth Create new Client Id» -> «Installed application» (Other)
- Copy and paste the CLIENT ID and CLIENT SECRET in GoogleDriveServiceProvider.java
Running the Project Locally
- Locate the App.java in src/main/java source folder and right-click on it->Run As->Run Configurations, and fill a folder name in Program Parameters, such as «/mydrive/» or «C:\doc».
- When the program is running, you can copy/modify/delete files in that folder and check if the changes have been synced to your Google Drive account.
- In order to simplify the function, all the files will be sync-ed to the root folder in your Google Drive.
- Only the first-level files will be monitored in your local folder. Sub-directories will not be monitored.
- The project has been configured to use JDK 7. If you use other versions installed, please modify the pom.xml file. However, JDK 7+ (or 8) is recommended.
To learn the core techniques used in this program, please read:
About
A simple version of Google Drive client in Java.
Java quickstart
Quickstarts explain how to set up and run an app that calls a Google Workspace API.
Google Workspace quickstarts use the API client libraries to handle some details of the authentication and authorization flow. We recommend that you use the client libraries for your own apps. This quickstart uses a simplified authentication approach that is appropriate for a testing environment. For a production environment, we recommend learning about authentication and authorization before choosing the access credentials that are appropriate for your app.
Create a Java command-line application that makes requests to the Google Drive API.
Objectives
Prerequisites
Set up your environment
To complete this quickstart, set up your environment.
Enable the API
Configure the OAuth consent screen
If you’re using a new Google Cloud project to complete this quickstart, configure the OAuth consent screen and add yourself as a test user. If you’ve already completed this step for your Cloud project, skip to the next section.
- In the Google Cloud console, go to Menu menu >APIs & Services >OAuth consent screen. Go to OAuth consent screen
- Select the user type for your app, then click Create.
- Complete the app registration form, then click Save and Continue.
- For now, you can skip adding scopes and click Save and Continue. In the future, when you create an app for use outside of your Google Workspace organization, you must add and verify the authorization scopes that your app requires.
- If you selected External for user type, add test users:
- Under Test users, click Add users.
- Enter your email address and any other authorized test users, then click Save and Continue.
Authorize credentials for a desktop application
- In the Google Cloud console, go to Menu menu >APIs & Services >Credentials. Go to Credentials
- Click Create Credentials >OAuth client ID.
- Click Application type >Desktop app.
- In the Name field, type a name for the credential. This name is only shown in the Google Cloud console.
- Click Create. The OAuth client created screen appears, showing your new Client ID and Client secret.
- Click OK. The newly created credential appears under OAuth 2.0 Client IDs.
- Save the downloaded JSON file as credentials.json , and move the file to your working directory.
Prepare the workspace
gradle init --type basic mkdir -p src/main/java src/main/resources
apply plugin: ‘java’ apply plugin: ‘application’ mainClassName = ‘DriveQuickstart’ sourceCompatibility = 11 targetCompatibility = 11 version = ‘1.0’ repositories < mavenCentral() >dependencies
Set up the sample
- In the src/main/java/ directory, create a new Java file with a name that matches the mainClassName value in your build.gradle file.
- Include the following code in your new Java file:
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.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.gson.GsonFactory; import com.google.api.client.util.store.FileDataStoreFactory; import com.google.api.services.drive.Drive; import com.google.api.services.drive.DriveScopes; import com.google.api.services.drive.model.File; import com.google.api.services.drive.model.FileList; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.security.GeneralSecurityException; import java.util.Collections; import java.util.List; /* class to demonstrate use of Drive files list API */ public class DriveQuickstart < /** * Application name. */ private static final String APPLICATION_NAME = "Google Drive API Java Quickstart"; /** * Global instance of the JSON factory. */ private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); /** * Directory to store authorization tokens for this application. */ private static final String TOKENS_DIRECTORY_PATH = "tokens"; /** * Global instance of the scopes required by this quickstart. * If modifying these scopes, delete your previously saved tokens/ folder. */ private static final ListSCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY); private static final String CREDENTIALS_FILE_PATH = "/credentials.json"; /** * Creates an authorized Credential object. * * @param HTTP_TRANSPORT The network HTTP Transport. * @return An authorized Credential object. * @throws IOException If the credentials.json file cannot be found. */ private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException < // Load client secrets. InputStream in = DriveQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH); if (in == null) < throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH); >GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); // Build flow and trigger user authorization request. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) .setAccessType("offline") .build(); LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); //returns an authorized Credential object. return credential; > public static void main(String. args) throws IOException, GeneralSecurityException < // Build a new authorized API client service. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) .setApplicationName(APPLICATION_NAME) .build(); // Print the names and IDs for up to 10 files. FileList result = service.files().list() .setPageSize(10) .setFields("nextPageToken, files(id, name)") .execute(); Listfiles = result.getFiles(); if (files == null || files.isEmpty()) < System.out.println("No files found."); >else < System.out.println("Files:"); for (File file : files) < System.out.printf("%s (%s)\n", file.getName(), file.getId()); >> > >
Run the sample
- If you’re not already signed in to your Google Account, you’re prompted to sign in. If you’re signed in to multiple accounts, select one account to use for authorization.
- Click Accept.
Authorization information is stored in the file system, so the next time you run the sample code, you aren’t prompted for authorization.
You have successfully created your first Java application that makes requests to the Google Drive API.
Next steps
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-07-12 UTC.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
mmiele/google-drive-client-java
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
This example demonstrates how to build a Google Drive client application in Java. This is a simple command line client application that eliminates unnecessary clutter and shows the basic logic to interact with Google Drive. Hopefully this will help you to understand the syntax (and semantic) of the API.
The application interacts with the Google Drive via its REST API using the related Google Java client library. For more information, see Google API Client Libraries. See also, The REST API Conundrum.
The application uses a custom client authentication application to be authorized to use the service.
The application assumes that a file exists that contains default settings. The file is assumed to be at ~/.store/drive_sample/sample_settings.json (C:\Users\USER_NAME.store\drive_sample\sample_settings.json for Windows users). The settigs are in Jason format similar to the following:
- From the project, create an executable JAR
- From a terminal window, go to the directory containing the JAR and execute a command similar to the following:
java -jar google-drive-client-java.jar