【问题标题】:Error in updating rows Google Sheets API更新行 Google Sheets API 时出错
【发布时间】:2016-07-18 23:05:47
【问题描述】:

我尝试更新 Google 表格中的行,但出现错误 403

请求的身份验证范围不足。 [403] 错误 [消息 [请求的身份验证范围不足。] Location[ - ] Reason[forbidden] Domain[global]]

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);
        }

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

        List<object> list1 = new List<object>() { "Item", "Cost", "Stocked", "Ship Date" };
        List<object> list2 = new List<object>() { "Wheel", "$20.50", "4", "3/1/2016" };
        List<object> list3 = new List<object>() { "Door", "$15", "2", "3/15/2016" };
        List<object> list4 = new List<object>() { "Engine", "$100", "1", "30/20/2016" };
        List<object> list5 = new List<object>() { "Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)" };
        IList<IList<Object>> list = new List<IList<Object>>() { list1, list2, list3, list4, list5 };

        ValueRange VRange = new ValueRange();
        VRange.Range = range;
        VRange.Values = list;


        SpreadsheetsResource.ValuesResource.UpdateRequest upd = service.Spreadsheets.Values.Update(VRange, spreadsheetId, range);
        UpdateValuesResponse response = upd.Execute();
        Console.WriteLine(response.UpdatedRows);

【问题讨论】:

    标签: c# google-api google-sheets


    【解决方案1】:

    Google 会缓存验证,因此一旦您通过浏览器弹出窗口批准客户端,它就不会再次出现。但是,当您更改它不检查的范围时,它只是继续使用已经批准(存储)的凭据,无论以前的权限可能是什么。

    示例代码在评论中确实提到了这一点:

    如果修改这些范围,请删除您之前保存的凭据 在 ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json

    由于您正在写信给System.Environment.SpecialFolder.Personal,它会显示在您的/Documents 文件夹中,位于.credentials/sheets.googleapis.com-dotnet-quickstart.json 下,每个Path.Combine。删除该文件(或您喜欢的文件夹)并确保使用static string[] Scopes = { SheetsService.Scope.Spreadsheets };,它现在会再次提示您,您应该一切就绪。

    【讨论】:

    • 是的,这就是对我有用的解决方案,删除 My Documents 中 .Credentials 文件夹下的凭据文件夹。
    • 您好,我在 Documents 文件夹中找不到 .Credentials 文件夹。新 api 的位置是否改变了?
    【解决方案2】:

    我看不到您在代码中定义范围的行,但请确保您没有请求 READONLY 范围

    【讨论】:

    • 我在static string[] Scopes = { SheetsService.Scope.Spreadsheets };上更正了范围,但它不再起作用了
    • 您可以执行读取功能吗?开发控制台中是否启用了 Drive Api?也可以尝试重新制作 oauth 令牌。
    猜你喜欢
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 2018-12-19
    • 1970-01-01
    • 2020-01-30
    相关资源
    最近更新 更多