Google Sheets Api Request had insufficient authent

2019-08-18 23:50发布

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,
    });
}

This is my code for Google Sheets API. I have the static string[] Scopes = { SheetsService.Scope.Spreadsheets }; which were solutions I found for this problem, however that did not fix my problem. I have already deleted my .credentials folder and it does not prompt me to login.

1条回答
Luminary・发光体
2楼-- · 2019-08-19 00:26
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            Scopes,
            "user",
            CancellationToken.None
            //new FileDataStore(credPath, true)
            ).Result;

I commented out that line and it fixed everything!

查看更多
登录 后发表回答