Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Request had insufficient authentication scopes." #110

Open
testing-blaze opened this issue Feb 18, 2021 · 1 comment
Open

"Request had insufficient authentication scopes." #110

testing-blaze opened this issue Feb 18, 2021 · 1 comment

Comments

@testing-blaze
Copy link

I am able to read my values. However when I am trying to update any cell i get subject error.

Sample Name

QuickStarts

Expected Behavior

Performing updating of cell and data should be updated to the cell.

Actual Behavior

Getting below error

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
PUT https://sheets.googleapis.com/v4/spreadsheets/1sCHkcsZJ7CjgQPyHjkmqKtHyHgWNE/values/B4?valueInputOption=RAW
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"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:118)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:37)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:428)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1108)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:514)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:455)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:565)

Specifications

  • Java version (11)
  • OS (Mac)

Code Snippet

private static final String APPLICATION_NAME = "Google Sheets API";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
static final String spreadsheetId = "1sCHkcsZJ7CjgQPyHjkmqKtHyar0kj8ueeUHgWNE";
static final String range = "Product-Analyzer!A4:L4";
public static List<List> loadedValues = null;
private static Sheets service = null;

/**
 * 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 = Arrays.asList(SheetsScopes.SPREADSHEETS,SheetsScopes.DRIVE);
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 = GoogleSheetsConnect.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();
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}


public static void main(String... args) throws IOException, GeneralSecurityException {
    ValueRange body = new ValueRange().setValues(Arrays.asList(Arrays.asList("Test")));
    sheetService().spreadsheets().values().update(spreadsheetId, "B4", body).setValueInputOption("RAW").execute();

}

private static Sheets sheetService() throws IOException, GeneralSecurityException {
    if (service == null) {
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();
    }
    return service;
}

@LoveCredit
Copy link

There might be a 'token' folder. Rename it, then run your code again to force google web login.
See e.g. https://stackoverflow.com/questions/70569649/get-access-token-scope-insufficient-error-migrating-to-people-api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants