前提
C#からGoogle Sheets Apiを使用したアプリを開発しておりますが、書き込み時にエラーが発生しています。
Microsoft Visual Studio Community 2022
実現したいこと
原因と対処方法をご教示お願い致します。
発生している問題・エラーメッセージ
ハンドルされていない例外
Google.GoogleApiException: 'The service sheets has thrown an exception. HttpStatusCode is Forbidden. Request had insufficient authentication scopes.
該当のソースコード
Microsoft Visual Studio Community 2022
C#
static string[] Scopes1 = { SheetsService.Scope.Spreadsheets };
static string ApplicationName = "Google Sheets API .NET Quickstart";
private void button1_Click(object sender, EventArgs e) { UserCredential credential; using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)) { string credPath = "token.json"; credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.FromStream(stream).Secrets, Scopes1, "user", CancellationToken.None, new FileDataStore(credPath, true) ).Result; } var sheetsService = new SheetsService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); String spreadsheetId = "シートのID"; String range = "シート1!A1"; var wv = new List<IList<object>>() { new List<object>{ DateTime.Now.ToString(), "11", "22", "33", } }; var body = new ValueRange() { Values = wv }; var req = sheetsService.Spreadsheets.Values.Append(body, spreadsheetId, range); req.ValueInputOption = SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum.USERENTERED; var result = req.Execute(); <==ここでエラー
※ソースコード内のシートのIDは正しいものに書き換えた上で実行しています。
試したこと
このシートの読み取りはできています。
String range = "シート1!A1";
SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get(spreadsheetId, range);
ValueRange response = request.Execute();

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。