如何调试“调用者没有权限”

时间:2016-07-22 14:37:04

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

我正在使用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();
    }
}

1 个答案:

答案 0 :(得分:2)

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

要解决我遇到的问题:

  1. 退出所有帐户
  2. 删除在&#34;我的Documents.credentials \ sheets.googleapis.com-dotnet-quickstart.json \&#34;
  3. 中创建的令牌文件
  4. 登录正确的Google帐户
  5. 重新启动应用程序
  6. 现在应该打开一个浏览器窗口,并要求获得访问Google表格的权限
  7. 未经过测试,但我认为当所有Google帐户都可以访问相关表格时,我不会遇到任何问题。