1

Recently I was using google spreadsheet api to make values being automatically updated. I was able to read the values from cells, but when I'm trying to write them, the problem occurs. From the console I can actually see that I do not have required permissions (scopes) to do so. After some searches for solution I've noticed that I need to set more scopes, but even if I add every scope, it still doesn't work.

I don't use any VM or VPS, I'm just running the code from IntelliJ Ultimate.

This is my code:

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.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.UpdateValuesResponse;
import com.google.api.services.sheets.v4.model.ValueRange;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;

public class Writing {
    private static Sheets sheetsService;
    private static String APPLICATION_NAME = "API Test";
    private static String SPREADSHEET_ID = "mySpreadsheetID";

    private static Credential authorize() throws IOException, GeneralSecurityException {
        InputStream in = Writing.class.getResourceAsStream("/credentials.json");
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in));

        List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS, SheetsScopes.DRIVE, SheetsScopes.DRIVE_FILE);

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), clientSecrets, scopes)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File("tokens")))
                .setAccessType("offline")
                .build();
        Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");

        return credential;
    }

    public static Sheets getSheetsService() throws IOException, GeneralSecurityException {
        Credential credential = authorize();
        return new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), credential)
                .setApplicationName(APPLICATION_NAME)
                .build();
    }

    public static void main(String[] args) throws IOException, GeneralSecurityException {
        sheetsService = getSheetsService();
        Object foo = "randomValue";
        List<List<Object>> values = Arrays.asList(
                Arrays.asList(foo));
        ValueRange body = new ValueRange()
                .setValues(values);
        UpdateValuesResponse result =
                sheetsService.spreadsheets().values().update(SPREADSHEET_ID, "B3", body)
                        .setValueInputOption("RAW")
                        .execute(); //Line 58
        System.out.printf("%d cells updated.", result.getUpdatedCells());

    }
}

You can see that I'm trying to insert a randomValue into B3 cell.

And the error I get:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Request had insufficient authentication scopes.",
    "reason" : "forbidden"
  } ],
  "message" : "Request had insufficient authentication scopes.",
  "status" : "PERMISSION_DENIED"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at Writing.main(Writing.java:58)
ProbDed
  • 159
  • 1
  • 3
  • 11
  • 1
    Scopes looks ok, although using borh `SheetsScopes.DRIVE` and `SheetsScopes.DRIVE_FILE` is redundant. Have You tried to remove Your credentials file after changing scopes? This link might be helpful: https://stackoverflow.com/questions/38534801/google-spreadsheet-api-request-had-insufficient-authentication-scopes – Michal W Jun 16 '19 at 11:40
  • @www yes I deleted and added credentials.json file multiply times. I also deleted `target` folder in which are all .class files that are being build after running the application. Still doesn't work. – ProbDed Jun 16 '19 at 20:32

1 Answers1

0

I faced the same error and after numerous research on web I came up with below approach which is working for me for upload. It should work for update as well using other method instead of create. More info at github

    /** Uploads a file using either resumable or direct media upload. */
private static File uploadFile(boolean useDirectUpload) throws GeneralSecurityException, IOException {

    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    Drive drive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
            .setApplicationName(APPLICATION_NAME)
            .build();

    File fileMetadata = new File();

    fileMetadata.setName("My Report");
    fileMetadata.setMimeType("application/vnd.google-apps.spreadsheet");

    java.io.File filePath = new java.io.File("D:/exportData.csv");

    FileContent mediaContent = new FileContent("text/csv", filePath);

    Drive.Files.Create insert = drive.files().create(fileMetadata, mediaContent);
    MediaHttpUploader uploader = insert.getMediaHttpUploader();
    uploader.setDirectUploadEnabled(useDirectUpload);
    uploader.setProgressListener(new FileUploadProgressListener());
    //System.out.println("File ID: " + file.getId());
    return insert.execute();
}