Java google drive client

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.

  1. Install the Eclipse 4+ ( http://www.eclipse.org/ ), and make sure Maven plugin is available.
  2. (Optional) The Maven plugin is not availble in your Eclipse, install it using the update site ( http://www.eclipse.org/m2e/download/ ).
  3. Install the latest Maven command-line tool ( http://maven.apache.org/download.cgi )

Importing the Project into Eclipse

  1. File->Import->Maven->Existing Maven Projects
  2. Select the directory containing the pom.xml file
  3. Finish

Building the Project for the First Time

Configure Google Drive API Credential

  1. 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
  2. When creating the Client ID, choose: «OAuth Create new Client Id» -> «Installed application» (Other)
  3. Copy and paste the CLIENT ID and CLIENT SECRET in GoogleDriveServiceProvider.java

Running the Project Locally

  1. 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».
  2. 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.
  1. In order to simplify the function, all the files will be sync-ed to the root folder in your Google Drive.
  2. Only the first-level files will be monitored in your local folder. Sub-directories will not be monitored.
  3. 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.
Читайте также:  Viewing html in safari

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

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.

  1. In the Google Cloud console, go to Menu menu >APIs & Services >OAuth consent screen. Go to OAuth consent screen
  2. Select the user type for your app, then click Create.
  3. Complete the app registration form, then click Save and Continue.
  4. 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.
  5. If you selected External for user type, add test users:
    1. Under Test users, click Add users.
    2. Enter your email address and any other authorized test users, then click Save and Continue.

    Authorize credentials for a desktop application

    1. In the Google Cloud console, go to Menu menu >APIs & Services >Credentials. Go to Credentials
    2. Click Create Credentials >OAuth client ID.
    3. Click Application type >Desktop app.
    4. In the Name field, type a name for the credential. This name is only shown in the Google Cloud console.
    5. Click Create. The OAuth client created screen appears, showing your new Client ID and Client secret.
    6. Click OK. The newly created credential appears under OAuth 2.0 Client IDs.
    7. 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

    1. In the src/main/java/ directory, create a new Java file with a name that matches the mainClassName value in your build.gradle file.
    2. 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

    1. 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.
    2. 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:

    1. From the project, create an executable JAR
    2. 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

    Источник

Оцените статью