Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Sheets API V4 - Update public sheet without OAuth

I'm trying to programmatically update a public spreadsheet (set to anyone can edit) via the API but it fails with

401 - "The request does not have valid authentication credentials."

I would expect to not need "valid authentication credentials" since it's a publicly editable spreadsheet. I can GET data from the sheet just fine, although I had to generate a "browser" API Key since apparently using an Android Key doesn't work.

Anyone know if there is a trick to getting an update to work, or is this not possible with the API?

Sample code I'm hacking together:

// Don't think I even need this?
GoogleCredential credential = new GoogleCredential();
credential.createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS));

HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory factory = JacksonFactory.getDefaultInstance();
final Sheets sheets = new Sheets.Builder(transport, factory, credential)
            .setApplicationName("My Awesome App")
            .build();
final String sheetID = "[ID Of Valid Public Spreadsheet Here]";
final String range = "A:S";
final ValueRange content = new ValueRange();
content.set("Column A Name", "Some Value to Set");
new Thread() {
    @Override
    public void run() {
        try {
            UpdateValuesResponse valueRange = sheets.spreadsheets().values()
                    .update(sheetID, range, content)
                    .setKey("My-Valid-Browser-Api-Key")
                    .execute();
                mLog.D("Got values: " + valueRange);
            }
            catch (IOException e) {
                mLog.E("Sheets failed", e);
            }
        }
    }.start();
like image 566
dominicoder Avatar asked Jul 19 '16 02:07

dominicoder


People also ask

How to request access to the Google Sheets API using OAuth?

To request access using OAuth 2.0, your application needs the scope information, as well as information that Google supplies when you register your application (such as the client ID and the client secret). Requests to the Google Sheets API for public data must be accompanied by an identifier, which can be an API key or an access token.

Do I need an API key for my Google Sheets application?

When your application requests public data, the request doesn't need to be authorized, but does need to be accompanied by an identifier, such as an API key. Every request your application sends to the Google Sheets API needs to identify your application to Google.

How to integrate Google Sheets API with Google Cloud Platform?

You need to create a new or use existing Google Cloud Platform project. Enable Sheets API in API Library. Make sure Credential - OAuth 2.0 client IDs contain an entry with Package name (refer to applicationId in Module:app build.gradle)

How do I identify my application to Google Sheets?

Every request your application sends to the Google Sheets API needs to identify your application to Google. There are two ways to identify your application: using an OAuth 2.0 token (which also authorizes the request) and/or using the application's API key.


1 Answers

The Sheets V4 API today does not allow anonymous edits, even for sheets that allow it. (It does allow anonymous reads for sheets that allow it.)

like image 176
Sam Berlin Avatar answered Sep 22 '22 01:09

Sam Berlin