Google drive service. How to approach this

I am trying to start by example from google, but still getting exeption “Resource not found”. Sorry for question, i am new to cuba as well as java :slight_smile:

This is source of example: راه اندازی سریع جاوا  |  Google Drive  |  Google Developers

Example service code:

package com.company.ramcovezmluvy.service;

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.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 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;

public interface GoogleDriveService {
    String NAME = "ramcovezmluvy_GoogleDriveService";

    class DriveQuickstart {

        private static final String APPLICATION_NAME = "Google Drive API Java Quickstart";
        private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
        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 List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
        private static final String CREDENTIALS_FILE_PATH = "google_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();
            return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
        }

        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();
            List<File> files = 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());
                }
            }
        }
    }
}

And this is exeption:

---- IntelliJ IDEA coverage runner ---- 
sampling ...
include patterns:
com\.company\.ramcovezmluvy\.service\..*
exclude patterns:
Exception in thread "main" java.io.FileNotFoundException: Resource not found: google_credentials.json
	at com.company.ramcovezmluvy.service.GoogleDriveService$DriveQuickstart.getCredentials(GoogleDriveService.java:53)
	at com.company.ramcovezmluvy.service.GoogleDriveService$DriveQuickstart.main(GoogleDriveService.java:70)
Class transformation time: 0.0186421s for 745 classes or 2.502295302013423E-5s per class

Process finished with exit code 1

Credentials file is located here(tried all options from example in cuba docs, but still same exeption):

Hi,

First, this particular topic does not relate to CUBA at all.
You’d find better and quicker help in the Google Drive API support resources: 如何取得協助  |  Google Drive  |  Google Developers

Then, you are loading resource with the java.lang.Class#getResourceAsStream method.
Let’s read javadocs for this method:

Finds a resource with a given name.

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
If the name begins with a ‘/’ (‘\u002f’), then the absolute name of the resource is the portion of the name following the ‘/’.
Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with ‘/’ substituted for ‘.’ (‘\u002e’).

Your class where you’re loading resource from is com.company.ramcovezmluvy.service.GoogleDriveService.DriveQuickstart, located in the package com.company.ramcovezmluvy.service.

Your resource is located in the com.company.ramcovezmluvy.resources package - another package!

So you have two choices:

  1. Move google_credentials.json to the com.company.ramcovezmluvy.service package, so that it’s located nearby GoogleDriveService.
    Then you will be able to load resource by specifying only file name.

  2. Load resource this way, specifying absolute path:

InputStream in = DriveQuickstart.class.getResourceAsStream(
    "/com/company/ramcovezmluvy/resources/google/google_credentials.json");

Thank you Alex.