gpt4 book ai didi

c# - 请求的身份验证范围不足。 【403】c#

转载 作者:行者123 更新时间:2023-12-02 00:08:33 25 4
gpt4 key购买 nike

我有两种方法。一个是从 Google Sheets 读取数据,另一个是向单个单元格添加值(但我实际上想将数据附加到最后一行,但是,我首先只从 1 条记录开始)。 btnLoadAttendance 工作完美,但是当我在 textbox1.Text 中的“UpdateValuesResponse result2 = update.Execute();”中输入某些内容时我收到如下错误:

Request had insufficient authentication scopes. [403]

我在这里找到了一个答案“Request had insufficient authentication scopes [403] when update cell spreadsheet”,它说关于评论一些行。我尝试评论该行,但它对我不起作用。我仍然遇到同样的错误。下面是我的代码:

工作

private void btnLoadAttendance_Click(object sender, EventArgs e)
{
UserCredential credential;

using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}

var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});

String spreadsheetId = "1b64mhUgdeRzGyJF4NfAPhkFY7br3c0o9rJ9mMnDBTR8";
String range = "Sheet1!A2:D";
SpreadsheetsResource.ValuesResource.GetRequest request =
service.Spreadsheets.Values.Get(spreadsheetId, range);

ValueRange response = request.Execute();
IList<IList<Object>> values = response.Values;
if (values != null && values.Count > 0)
{
foreach (var x in values)
{

dgvAttendance.Rows.Add(x[0], x[1], x[2], x[3]);
}
}
else
{
MessageBox.Show("No data found.");
}
}

不工作

private void btnAddData_Click(object sender, System.EventArgs e)
{
string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
UserCredential credential;

using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}

var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});

String spreadsheetId = "1b64mhUgdeRzGyJF4NfAPhkFY7br3c0o9rJ9mMnDBTR8/edit";
String range = "Sheet1!F5"; // update the F5 cell

//ValueRange response = request.Execute();
ValueRange valueRange = new ValueRange();
valueRange.MajorDimension = "COLUMNS"; //Rows or Columns

var oblist = new List<object>() { textBox1.Text };
valueRange.Values = new List<IList<object>> { oblist };

SpreadsheetsResource.ValuesResource.UpdateRequest update =
service.Spreadsheets.Values.Update(valueRange, spreadsheetId, range);
update.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
UpdateValuesResponse result2 = update.Execute();
}

非常感谢您的帮助。

最佳答案

  1. 首先删除存储在 c:\user\Documents.credentials 中的凭据文件 .credentials/sheets.googleapis.com-dotnet-quickstart.json。
  2. 更改用于从 Google 电子表格读取单元格的范围变量

    string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };

    静态字符串[]范围= { SheetsService.Scope.Spreadsheets };

  3. 执行代码后,API 将再次进行身份验证,然后问题将得到解决。

关于c# - 请求的身份验证范围不足。 【403】c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41356466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com