Android Google Sheets API V4 - Update public sheet

2019-04-28 22:50发布

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();

1条回答
Anthone
2楼-- · 2019-04-28 22:55

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.)

查看更多
登录 后发表回答