从.NET写入电子表格时,身份验证范围不足

时间:2018-09-19 15:50:29

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

尝试从.NET写入Google工作表。

有例外:

An unhandled exception of type 'Google.GoogleApiException' occurred in mscorlib.dll

Additional information: Google.Apis.Requests.RequestError

Request had insufficient authentication scopes. [403]

Errors [

    Message[Request had insufficient authentication scopes.] Location[ - ] Reason[forbidden] Domain[global]

]

具有读取功能的模拟功能可以正常工作。创建凭据并启用Google表格API。创建有关读/写的凭据时,我看不到任何设置。电子表格共享给每个读/写的人。如何解决这个问题?

static void UpdateEntry()
        {
            string  spreadsheetId = "1IAD2okAWZD7anbt5L4ybgD2dxHBGmsY6IkNIWHBQkBM";
            string ApplicationName = "myapp";
            UserCredential credential;

            using (var stream = new FileStream("credentials_1.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = "token.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,
            });




            String range = "new first sheet!A1"; 
            //var range = "{sheet}!D543";
            var valueRange = new ValueRange();

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

            var updateRequest = service.Spreadsheets.Values.Update(valueRange, spreadsheetId, range);
            updateRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED;
            var appendReponse = updateRequest.Execute();
        }

0 个答案:

没有答案