Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write data to Google Spreadsheet using API Key (C#)

I have an app that allow me to read the data from Google Spreadsheet using API Key. I just make HTTP GET to this address and get a response with data.

https://sheets.googleapis.com/v4/spreadsheets/18soCZy9H4ZGuu**********BeHlNY1lD8at-Pbjmf8c/values/Sheet1!A1?key=AIzaSyAYJ***********pB-4iKZjYf4y0vhXP8OM

But when I try to do same to write data using HTTP PUT to address

https://sheets.googleapis.com/v4/spreadsheets/18soCZy9H4ZGuu**********BeHlNY1lD8at-Pbjmf8c/values/Sheet1!A4?valueInputOption=RAW?key=AIzaSyAYJ***********pB-4iKZjYf4y0vhXP8OM

its gives me 401 error. Code to make PUT request:

 using (WebClient wc = new WebClient())
 {
     byte[] res = wc.UploadData(link, "PUT", Encoding.ASCII.GetBytes(textBox1.Text));
     MessageBox.Show(Encoding.Default.GetString(res));
 }

Also spreadsheet is fully public with permission to read and write by anyone without auth. My guess is that I can't use API Key to write data to spreadsheet, and only way to do this is using OAuth.

UPDATE: So i've just tryed Google.Apis.Sheets.v4 to write values, and now i'm almost 100% sure that API Key can't be used to write data to Google Spreadsheet. Well, then I'll use OAuth 2.0.

like image 905
Yura Belov Avatar asked Feb 03 '17 01:02

Yura Belov


People also ask

Can Google Sheets pull data from an API?

Once you've set up your API to Google Sheets connection, click Save And Run to get data to your spreadsheet.

How do I push data into Google Sheets?

Enter text or data: Click a cell and enter text. Insert more items: Click Insert and add charts, images, drawings, functions, notes, and more. Note: You can also add a function to a cell by typing =. To see which functions are available, see the Google spreadsheets function list.


2 Answers

Well, maybe you are correct and the problem here is the API_KEY itself.

If you check the Sheets API documentation, it is stated that 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. Here's how to determine which of those options to use:

  • If the request requires authorization (such as a request for an individual's private data), then the application must provide an OAuth 2.0 token with the request. The application may also provide the API key, but it doesn't have to.

  • If the request doesn't require authorization (such as a request for public data), then the application must provide either the API key or an OAuth 2.0 token, or both—whatever option is most convenient for you.

So meaning either the OAuth 2.0 token or API key will work in your case since the file is public. But the problem is in the PUT request that you are doing, we can assume here that the API key is not working with it. But, we have alternatives for it, and that is the OAuth.

I also found here a related SO question that might help you.

like image 150
KENdi Avatar answered Sep 23 '22 07:09

KENdi


For anyone still hoping for a simple answer, it seems there won't be one - any writing to a sheet, irrespective of the sheets permissions, will require OAuth2:

'This is intentional behavior. While public sheets are anonymously readable, anonymous edits aren't currently supported for a variety of reasons.

In this context, "anyone" == anyone with a google account.' HERE

like image 31
sideroxylon Avatar answered Sep 21 '22 07:09

sideroxylon