Google表格Api请求的身份验证范围不足

时间:2017-12-01 05:36:17

标签: c# google-sheets-api

static string[] Scopes = { SheetsService.Scope.Spreadsheets };
static string ApplicationName = "Google Sheets API .NET Quickstart";
public String spreadsheetId;
SheetsService service;

public GoogleHandler(String id)
{
    spreadsheetId = id;
    UserCredential credential;
    using (var stream =
            new FileStream(@"bin\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;
    }

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

这是我的Google表格API代码。我有static string[] Scopes = { SheetsService.Scope.Spreadsheets };这是我为这个问题找到的解决方案,但是这并没有解决我的问题。我已经删除了我的.credentials文件夹,但它没有提示我登录。

1 个答案:

答案 0 :(得分:2)

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

我注释掉了那条线并修复了所有内容!