50

I am trying to update Google sheet values.

"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential."

I want to do this using API key not outh 2.0

Can anyone have any suggestions.

Rana Ghosh
  • 4,514
  • 5
  • 23
  • 40

2 Answers2

19

Not possible. You need to use the OAuth login as indicated here in spreadsheets.values.batchUpdate:

You can see on the authorization part that it uses OAuth scopes, therefore it follows that it uses OAuth not API KEY:

Authorization

Requires one of the following OAuth scopes:

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/spreadsheets
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • Thank you for your answer. Let me check with service account auth. – Rana Ghosh Oct 04 '17 at 15:13
  • 1
    I already have `https://www.googleapis.com/auth/spreadsheets` but it does not work when run call Sheets API from `onEdit()`, on edit a cell. What am I missing? When I run it manually, it works fine. Also, I have `Execute` button disabled – Kirby Jun 30 '22 at 15:48
5

I am facing similar type of issue.

Issue Analysis: Actual issue is missing of scope.

Solution: Scope can be added by the following ways:

  1. using enabling api
  2. Adding oauth scope list. List is available here: https://developers.google.com/identity/protocols/googlescopes
  3. Adding scope from Code level

Details are available here:

  1. If any scope is missing, first we need to check that related api is enabled or not. If not enabled yet, go first on google console and enable api.

Procedure is available here: https://support.google.com/googleapi/answer/6158841?hl=en

  1. After enabling api, sometimes you will not get your expected scope. If there is any sophisticated scope is required, you need to add those in your scope list. For this, you need to follow the procedure:

    i) Go to google console

    ii) Select your project

    iii) In left sidebar, you will get "Oauth consent screen". Click on that button

    iv) You will get "Add scope" button. There is actually 3 is enlisted primarily: email, profile and openID

    v) You can add your expected scope here.

  2. In code level, we can also add SCOPES. In code level, I am using singleton list. Which is always using single scope. So when I need another scopes like user profile or user email. I can't reach those. So I changed it and got my expected result.

Previous Code:

private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE);

After change:

private static final List<String> SCOPES = new ArrayList<>(
            Arrays.asList(DriveScopes.DRIVE,
                    Oauth2Scopes.USERINFO_EMAIL,
                    Oauth2Scopes.USERINFO_PROFILE,
                    Oauth2Scopes.PLUS_ME));
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • 1
    Is there a tutorial somewhere for beginners on how to handle authentication oauth? I know nothing about it and don't know where to start. – Robert M. May 29 '23 at 00:12