【问题标题】:How to debug "The caller does not have permission"如何调试“调用者没有权限”
【发布时间】:2016-07-22 14:37:04
【问题描述】:

我使用的是 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();
    }
}

【问题讨论】:

  • 发布您的代码。 正在使用的确切代码。
  • 我已经添加了代码。

标签: c# google-sheets-api google-api-dotnet-client


【解决方案1】:

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

为了解决我所做的问题:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 2017-04-04
    • 2019-08-06
    • 2020-02-26
    • 2019-05-15
    • 1970-01-01
    相关资源
    最近更新 更多