如何调试 "The caller does not have permission"

How to debug "The caller does not have permission"

我正在使用 Google 表格 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 帐户询问令牌。

为了解决我所做的问题:

  1. 注销所有帐户
  2. 删除在"My Documents.credentials\sheets.googleapis.com-dotnet-quickstart.json\"
  3. 中创建的令牌文件
  4. 登录正确的 Google 帐户
  5. 重新启动应用程序
  6. 浏览器 window 现在应该会打开并请求访问 Google 表格
  7. 的权限

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