与语法“更新”相关的身份验证范围不足

时间:2018-03-21 09:40:16

标签: c# wpf oauth-2.0 google-sheets-api

目标:
使用Google Sheet API使用语法代码“spreadsheets.values.update”以更新当前单元格。

问题:
我使用了Google Sheet的教程(https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update)提供的代码,但它不起作用。

我检索到错误消息:

"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]

]"

的信息:
*我可以使用与语法代码“spreadsheets.values.get”(https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get)相关的Oath 2.0身份验证,并且它运行良好。
*“spreadsheets.values.get”中使用的Oath 2.0的相同代码效果很好 *当我用于“获取”和“更新”时,它是相同的身份验证。它适用于“获取”,但不适用于“更新”。

谢谢!

enter image description here

private void btn_test6_Click(object sender, RoutedEventArgs e)
{

    string[] Scopes = { SheetsService.Scope.Spreadsheets};
    string ApplicationName = "SheetUpdate"; //update this!


    UserCredential credential;

    using (var stream =
        new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
    {
        string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            Scopes,
            "user",
            CancellationToken.None,
            new FileDataStore(credPath, true)).Result;
        Console.WriteLine("Credential file saved to: " + credPath);
    }


    SheetsService sheetsService = new SheetsService(new BaseClientService.Initializer
    {
        HttpClientInitializer = credential,
        ApplicationName = "Google-SheetsSample/0.1",
    });

    // The ID of the spreadsheet to update.
    string spreadsheetId = "";  // TODO: Update placeholder value.

    // The A1 notation of the values to update.
    string range = "datadata!A1:A6";  // TODO: Update placeholder value.

    // How the input data should be interpreted.
    SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum valueInputOption = (SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum)0;  // TODO: Update placeholder value.



    IList<object> my = new List<object>();
    my.Add("a");
    my.Add("a");
    my.Add("a");
    my.Add("a");
    my.Add("a");
    my.Add("a");

    IList<IList<object>> my2 = new List<IList<object>>();
    my2.Add(my);



    // TODO: Assign values to desired properties of `requestBody`. All existing
    // properties will be replaced:
    Google.Apis.Sheets.v4.Data.ValueRange requestBody = new Google.Apis.Sheets.v4.Data.ValueRange();
    requestBody.Values = my2;






    SpreadsheetsResource.ValuesResource.UpdateRequest request = sheetsService.Spreadsheets.Values.Update(requestBody, spreadsheetId, range);
    request.ValueInputOption = valueInputOption;

    // To execute asynchronously in an async method, replace `request.Execute()` as shown:
    Google.Apis.Sheets.v4.Data.UpdateValuesResponse response = request.Execute();
    // Data.UpdateValuesResponse response = await request.ExecuteAsync();

    // TODO: Change code below to process the `response` object:
    Console.WriteLine(JsonConvert.SerializeObject(response));
}

3 个答案:

答案 0 :(得分:0)

删除新的FileDataStore

        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            Scopes,
            "user",
            CancellationToken.None //,
                                   //new FileDataStore(credPath, true)
            ).Result;

答案 1 :(得分:0)

更新需要以下授权范围

https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/spreadsheets

请参阅here

尝试修改数组中的范围,并在Google控制台的OAuth consent screen中更新应用程序/项目的权限,并在其中添加这些范围。

答案 2 :(得分:0)

替换此行:

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

与此:

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

如果未通过,请删除(token.json)文件夹,然后重试