gpt4 book ai didi

c# - 如何调试 "The caller does not have permission"

转载 作者:行者123 更新时间:2023-11-30 23:23:05 25 4
gpt4 key购买 nike

我使用的是 Google Sheets API v4。

我已经用一些工作表进行了测试,我的代码可以正常工作,但有些则不行。我所做的只是换出工作表 ID。

我使用在这里找到的代码:

https://developers.google.com/sheets/quickstart/dotnet

我测试的所有工作表都可以在我的浏览器中阅读和编辑。我遇到的错误是:

Google.Apis.Requests.RequestError\r\nThe caller does not have permission [403]\r\nErrors [\r\n\tMessage[The caller does not have permission] Location[ - ] Reason[forbidden] Domain[global]\r\n]\r\n

调试此类问题的步骤是什么?

完整的源代码如下:

public class Google_Sheets
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
static string ApplicationName = "Google Sheets API Quickstart";

public static void Test()
{
UserCredential credential;

using (var stream =
new FileStream("Google Drive\\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);
}

// Create Google Sheets API service.
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});

// Define request parameters.
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit

// working
//String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
//String range = "Class Data!A2:E";

// not working
String spreadsheetId = "1KJtvpd8iROI7cZ-7--fyoTXTLjzdb8ZghmmQOWkKsh4";
String range = "Sheet1!A1:G";

SpreadsheetsResource.ValuesResource.GetRequest request =
service.Spreadsheets.Values.Get(spreadsheetId, range);

// Prints the names and majors of students in a sample spreadsheet:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
ValueRange response = request.Execute();
IList<IList<Object>> values = response.Values;
if (values != null && values.Count > 0)
{
Console.WriteLine("Name, Major");
foreach (var row in values)
{
Console.WriteLine("{0}, {1}", row[0], row[1]);
}
}
else
{
Console.WriteLine("No data found.");
}
Console.Read();
}
}

最佳答案

原来我的问题是我登录了两个不同的 Google 帐户。我想我的工具随后向错误的 Google 帐户请求 token 。

为了解决我所做的问题:

  1. 退出所有账户
  2. 删除在“My Documents.credentials\sheets.googleapis.com-dotnet-quickstart.json\”中创建的 token 文件
  3. 登录正确的 Google 帐户
  4. 重启应用
  5. 现在应该会打开一个浏览器窗口并请求访问 Google 表格的权限

未经测试,但我假设当所有 Google 帐户都可以访问相关工作表时,我不会有任何问题。

关于c# - 如何调试 "The caller does not have permission",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38529162/

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