c# - 从 .NET 写入电子表格时身份验证范围不足
问题描述
试图从 .NET 写入谷歌表格。
有异常:
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]
]
带读取的模拟功能工作正常。创建凭据并启用 google sheet API。创建有关读/写的凭据时,我看不到任何设置。电子表格共享给每个读/写的人。如何解决这个问题呢?
static void UpdateEntry()
{
string spreadsheetId = "1IAD2okAWZD7anbt5L4ybgD2dxHBGmsY6IkNIWHBQkBM";
string ApplicationName = "myapp";
UserCredential credential;
using (var stream = new FileStream("credentials_1.json", FileMode.Open, FileAccess.Read))
{
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Sheets API service.
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
String range = "new first sheet!A1";
//var range = "{sheet}!D543";
var valueRange = new ValueRange();
var oblist = new List<object>() { "updated" };
valueRange.Values = new List<IList<object>> { oblist };
var updateRequest = service.Spreadsheets.Values.Update(valueRange, spreadsheetId, range);
updateRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
var appendReponse = updateRequest.Execute();
}
解决方案
推荐阅读
- google-apps-script - 有没有办法将工作表复制到另一个包含脚本功能的电子表格?
- python - 有没有办法在熊猫数据框中取消嵌套列表列?
- firebase - 暴露 Google Analytics Tag 对敏感数据造成的危害
- linux - 在我的 Amazon Linux 2 上释放一些内存空间
- c# - 如何在 C# WPF 中访问我的解决方案中的图像文件
- json - Python:请求的 .json() 方法不起作用
- html - CSS 转换过渡未按预期工作
- r - 如何摆脱ggplot barplot中突然出现的水平白线?
- c++ - 在 Visual Studio 中,是否有像 C# 一样的 C++ 代码注释?
- angular - Angular-Material:本机日期时间选择器组件,无秒