In case of Java / Google Sheets API V4 / API key authentication, data update process (update) is 401 error (OAuth authentication is required) Furthermore, there is no interface to pass credentials to Sheets object

If you can't, write that you can't. => Google

environment

item Version etc.
java 1.8
Sheets API Version 4
Spreadsheet ownership Anyone who owns and knows the URL can edit it

Symptoms

--Create a Google API Project with the account that owns the spreadsheet --Enable Sheets API --Create API key (assign to use Sheet API) --Access spreadsheets using API key --Read (get) is OK --401 error on write (update) --The exception is GoogleJsonResponseException --Error message: 401 Unauthorized

Cause

In the case of writing (although it is not written anywhere), it seems that API key authentication is not possible and OAuth2 authentication is required.

It's thanks to ʻAPI Explorer` that I got this conclusion even though I didn't write it anywhere. This is a common function that allows you to execute API requests from a browser, but you can also select and execute authentication methods as well as request parameter settings. As a result, a 401 error occurs with API Key authentication. It was successful with OAuth2 authentication.

Click here for API Explorer.

Request settings Authentication settings to use
Image from Gyazo ImagefromGyazo

reference

Failure response

{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

Reference / Success Response

{
  "spreadsheetId": "1aAk_HFTmn*********************************",
  "updatedRange": "'p01'!B3:M3",
  "updatedRows": 1,
  "updatedColumns": 12,
  "updatedCells": 12
}

[Sad news] The Sheets API Java library doesn't know how to pass OAuth2 credentials to a Sheets instance

In the QuickStart sample code, credential can be passed as the third argument of the Sheets.Builder constructor, but it is actually httpInitializer and cannot be passed (compile error).

I wondered if I could set it after builder () like bigQuery, but it didn't work. Creating a Sheets instance is a constructor that does not have all the samples. Google, please forgive me ...

**

BigQuery bigquery = 
       BigQueryOptions.newBuilder()
          ★.setCredentials(credentials)★
          .setProjectId(projectId)
          .build()
          .getService();

Since there is an interface called SetOAuthToken in HttpRequest of httpInitializer, should I use this?

It doesn't have to be Java anymore.

** Sample code **

Sheets service = 
       new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, ★getCredentials(HTTP_TRANSPORT)★)
           .setApplicationName(APPLICATION_NAME)
           .build();

** From actual / official API documentation **

https://developers.google.com/resources/api-libraries/documentation/sheets/v4/java/latest/

Builder(
  com.google.api.client.http.HttpTransport transport, 
  com.google.api.client.json.JsonFactory jsonFactory, 
  com.google.api.client.http.HttpRequestInitializer ★httpRequestInitializer★)

Recommended Posts

In case of Java / Google Sheets API V4 / API key authentication, data update process (update) is 401 error (OAuth authentication is required) Furthermore, there is no interface to pass credentials to Sheets object