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();
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.
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.
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)
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With