S
Sakura Data
Guest
Goal:
Use Google Sheet API by using the syntax code "spreadsheets.values.update" in order to update the current cell.
Problem:
I have used the code that is provded by Google Sheet's tutorial (https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update) and it doesn't work.
I retrieve a error message saying:
"An unhandled exception of type 'Google.GoogleApiException' occurred in mscorlib.dll
Additional information: Google.Apis.Requests.RequestError
Request had insufficient authentication scopes. [403]
Errors [
Message[Request had insufficient authentication scopes.] Location[ - ] Reason[forbidden] Domain[global]
]"
Info:
*I was enable to use Oath 2.0 authenfication in relation to syntax code "spreadsheets.values.get" (https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get) and it works perfectly.
*The same code of Oath 2.0 that is used in "spreadsheets.values.get" works well.
**It is the same authenfication when I use for "get" and "update". It works for "get" but not for "update".
Thank you!
private void btn_test6_Click(object sender, RoutedEventArgs e)
{
string[] Scopes = { SheetsService.Scope.Spreadsheets};
string ApplicationName = "SheetUpdate"; //update this!
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
SheetsService sheetsService = new SheetsService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Google-SheetsSample/0.1",
});
// The ID of the spreadsheet to update.
string spreadsheetId = ""; // TODO: Update placeholder value.
// The A1 notation of the values to update.
string range = "datadata!A1:A6"; // TODO: Update placeholder value.
// How the input data should be interpreted.
SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum valueInputOption = (SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum)0; // TODO: Update placeholder value.
IList<object> my = new List<object>();
my.Add("a");
my.Add("a");
my.Add("a");
my.Add("a");
my.Add("a");
my.Add("a");
IList<IList<object>> my2 = new List<IList<object>>();
my2.Add(my);
// TODO: Assign values to desired properties of `requestBody`. All existing
// properties will be replaced:
Google.Apis.Sheets.v4.Data.ValueRange requestBody = new Google.Apis.Sheets.v4.Data.ValueRange();
requestBody.Values = my2;
SpreadsheetsResource.ValuesResource.UpdateRequest request = sheetsService.Spreadsheets.Values.Update(requestBody, spreadsheetId, range);
request.ValueInputOption = valueInputOption;
// To execute asynchronously in an async method, replace `request.Execute()` as shown:
Google.Apis.Sheets.v4.Data.UpdateValuesResponse response = request.Execute();
// Data.UpdateValuesResponse response = await request.ExecuteAsync();
// TODO: Change code below to process the `response` object:
Console.WriteLine(JsonConvert.SerializeObject(response));
}
Continue reading...
Use Google Sheet API by using the syntax code "spreadsheets.values.update" in order to update the current cell.
Problem:
I have used the code that is provded by Google Sheet's tutorial (https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update) and it doesn't work.
I retrieve a error message saying:
"An unhandled exception of type 'Google.GoogleApiException' occurred in mscorlib.dll
Additional information: Google.Apis.Requests.RequestError
Request had insufficient authentication scopes. [403]
Errors [
Message[Request had insufficient authentication scopes.] Location[ - ] Reason[forbidden] Domain[global]
]"
Info:
*I was enable to use Oath 2.0 authenfication in relation to syntax code "spreadsheets.values.get" (https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get) and it works perfectly.
*The same code of Oath 2.0 that is used in "spreadsheets.values.get" works well.
**It is the same authenfication when I use for "get" and "update". It works for "get" but not for "update".
Thank you!
private void btn_test6_Click(object sender, RoutedEventArgs e)
{
string[] Scopes = { SheetsService.Scope.Spreadsheets};
string ApplicationName = "SheetUpdate"; //update this!
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
SheetsService sheetsService = new SheetsService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Google-SheetsSample/0.1",
});
// The ID of the spreadsheet to update.
string spreadsheetId = ""; // TODO: Update placeholder value.
// The A1 notation of the values to update.
string range = "datadata!A1:A6"; // TODO: Update placeholder value.
// How the input data should be interpreted.
SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum valueInputOption = (SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum)0; // TODO: Update placeholder value.
IList<object> my = new List<object>();
my.Add("a");
my.Add("a");
my.Add("a");
my.Add("a");
my.Add("a");
my.Add("a");
IList<IList<object>> my2 = new List<IList<object>>();
my2.Add(my);
// TODO: Assign values to desired properties of `requestBody`. All existing
// properties will be replaced:
Google.Apis.Sheets.v4.Data.ValueRange requestBody = new Google.Apis.Sheets.v4.Data.ValueRange();
requestBody.Values = my2;
SpreadsheetsResource.ValuesResource.UpdateRequest request = sheetsService.Spreadsheets.Values.Update(requestBody, spreadsheetId, range);
request.ValueInputOption = valueInputOption;
// To execute asynchronously in an async method, replace `request.Execute()` as shown:
Google.Apis.Sheets.v4.Data.UpdateValuesResponse response = request.Execute();
// Data.UpdateValuesResponse response = await request.ExecuteAsync();
// TODO: Change code below to process the `response` object:
Console.WriteLine(JsonConvert.SerializeObject(response));
}
Continue reading...