【问题标题】:Request had insufficient authentication scopes. [403] c#请求的身份验证范围不足。 [403] c#
【发布时间】:2016-12-28 06:22:48
【问题描述】:

我有两种方法。一种是从 Google 表格中读取数据,另一种是向单个单元格添加值(但我实际上想将数据附加到最后一行,但我将首先从 1 条记录开始)。 btnLoadAttendance 完美运行,但是当我在 textbox1.Text 中输入内容时,在“UpdateValuesResponse result2 = update.Execute();”中我收到这样的错误:

Request had insufficient authentication scopes. [403]

我在这里找到了一个答案“Request had insufficient authentication scopes [403] when update cell spreadsheet”,它说评论一些行。我尝试评论那条线,但它对我不起作用。我仍然得到同样的错误。以下是我的代码:

工作中

private void btnLoadAttendance_Click(object sender, EventArgs e)
    {
            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);
            }

            var service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            String spreadsheetId = "1b64mhUgdeRzGyJF4NfAPhkFY7br3c0o9rJ9mMnDBTR8";
            String range = "Sheet1!A2:D";
            SpreadsheetsResource.ValuesResource.GetRequest request =
                    service.Spreadsheets.Values.Get(spreadsheetId, range);

            ValueRange response = request.Execute();
            IList<IList<Object>> values = response.Values;
            if (values != null && values.Count > 0)
            {
                foreach (var x in values)
                {

                    dgvAttendance.Rows.Add(x[0], x[1], x[2], x[3]);
                }
            }
            else
            {
                MessageBox.Show("No data found.");
            }
        }

不工作

private void btnAddData_Click(object sender, System.EventArgs e)
        {
            string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
            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);
            }

            var service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            String spreadsheetId = "1b64mhUgdeRzGyJF4NfAPhkFY7br3c0o9rJ9mMnDBTR8/edit";
            String range = "Sheet1!F5"; // update the F5 cell

            //ValueRange response = request.Execute();
            ValueRange valueRange = new ValueRange();
            valueRange.MajorDimension = "COLUMNS"; //Rows or Columns

            var oblist = new List<object>() { textBox1.Text };
            valueRange.Values = new List<IList<object>> { oblist };

            SpreadsheetsResource.ValuesResource.UpdateRequest update =
                service.Spreadsheets.Values.Update(valueRange, spreadsheetId, range);
            update.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
            UpdateValuesResponse result2 = update.Execute();
        }

非常感谢您的帮助。

【问题讨论】:

    标签: c# google-spreadsheet-api


    【解决方案1】:
    1. 首先删除存储在 c:\user\Documents.credentials 的凭证文件 .credentials/sheets.googleapis.com-dotnet-quickstart.json。
    2. 更改用于从 Google 电子表格中读取单元格的范围变量

      string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };

      static string[] Scopes = { SheetsService.Scope.Spreadsheets };

    3. 执行代码后,API 将再次进行身份验证,然后问题将得到解决。

    【讨论】:

      【解决方案2】:

      我只需要改变:

      string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
      

      static string[] Scopes = { SheetsService.Scope.Spreadsheets };
      

      我花了一整天的时间!

      【讨论】:

      • 你也救了我的命。
      猜你喜欢
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 2016-04-28
      • 2017-08-09
      • 2023-03-13
      • 1970-01-01
      • 2021-08-07
      • 2021-10-20
      相关资源
      最近更新 更多