更新单元电子表格时,请求的认证范围不足[403] [英] Request had insufficient authentication scopes [403] when update cell spreadsheet

查看:154
本文介绍了更新单元电子表格时,请求的认证范围不足[403]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google Developer Consol中,图纸API已启用。我使用的是我用来访问电子表格的相同密钥。当我读取数据时一切正常。我得到一个请求在requestUp.Execute()上没有足够的认证范围错误。

In Google Developer Consol the Sheet API is enabled. I am using the same key I use to access spreadsheet.When I read data all work fine. I get a Request had insufficient authentication scopes error on the requestUp.Execute().

using System;
using System.Collections.Generic;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using Google.Apis.Sheets.v4;
using System.IO;
using System.Threading;


namespace SpreadSheetTest
{
    class Program
    {
        static string[] Scopes = { SheetsService.Scope.Spreadsheets };
        static string ApplicationName = "Google Sheets API .NET Quickstart";
        static void Main(string[] args)
        {
            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,
            });

            // Define request parameters.
            String spreadsheetId = "165WwBrowehv2FGzt3xUjAkyKeU2IKdQzQZ9gyaGNzV0";
            String range = "sheet1!A2:E2";
            SpreadsheetsResource.ValuesResource.GetRequest request =
                    service.Spreadsheets.Values.Get(spreadsheetId, range);


            ValueRange response = request.Execute();



            BatchUpdateValuesRequest body = new BatchUpdateValuesRequest();
            List<ValueRange> rangev = new List<ValueRange>();
            ValueRange vv = new ValueRange();
            vv.Range = "sheet1!A2:E2";
            IList<IList<object>> values = new List<IList<object>>();
            List<object> child = new List<object>();
            for (int i = 0; i < 5; i++)
            {
                child.Add(i);
            }
            values.Add(child);
            vv.Values = values;
            rangev.Add(vv);
            body.Data = rangev;

            SpreadsheetsResource.ValuesResource.BatchUpdateRequest requestUp =
                    service.Spreadsheets.Values.BatchUpdate(body, spreadsheetId);
            var result = requestUp.Execute();
        }
    }
}


推荐答案


  1. 首先删除凭证文件/Users/{YOUR_USER_NAME}/.credentials/sheets.googleapis.com-dotnet-quickstart.json(取决于您的设置)

  1. Firstly delete the credentials files /Users/{YOUR_USER_NAME}/.credentials/sheets.googleapis.com-dotnet-quickstart.json (depending on your setting)

更改用于从Google Spreadsheets中读取单元格的范围变量

Change the scope variable used for reading cells from Google Spreadsheets from




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

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

to



to


静态字符串[]范围= {SheetsService.Scope.Spreadsheets};

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




  1. 执行代码后,API将再次进行身份验证,然后问题将得到解决。
  1. After execution of code, API will authenticate again and then issue will be resolved.

这篇关于更新单元电子表格时,请求的认证范围不足[403]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆