C# 从Google Sheets API获取凭据

huangapple go评论127阅读模式
英文:

C# get credentials from Google Sheets API

问题

我遇到了从Google API获取凭据的问题。我搜索了很多旧话题,但找不到解决方法。

我的代码如下:

static readonly string[] Scopes = { SheetsService.Scope.Spreadsheets };
static readonly string ApplicationName = "Dot Tutorials";
static readonly string sheet = "Arkusz1";
static readonly string SpreadsheetId = "1IEvv4enfouBXrGEVLTKeQNYTJIzQHmRNyKTxKAxEAfg";
static SheetsService service;
GoogleCredential credential;
    //读取凭据文件...
    using (var stream = new FileStream("secret.json", FileMode.Open, FileAccess.Read))
    {
        credential = GoogleCredential.FromStream(stream)
            .CreateScoped(Scopes);
    }
    // 创建Google Sheets API服务...
    service = new SheetsService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName,
    });

我下载的secret.json如下:

{
  "installed": {
    "client_id": "1087609048092-hdh5ku57kaamha72o7t3tdtkb62vq5ur.apps.googleusercontent.com",
    "project_id": "excellent-well-151116",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "*********隐藏我的秘密代码*******",
    "redirect_uris": [
      "http://localhost"
    ]
  }
}

当我运行它时,仍然出现错误:

System.InvalidOperationException: 'Error creating credential from JSON or JSON parameters. Unrecognized credential type .'

我不知道这里出了什么问题。我尝试遵循这个教程:https://dottutorials.net/google-sheets-read-write-operations-dotnet-core-tutorial/

我的目标是从这个电子表格中获取值到一个DataTable:https://docs.google.com/spreadsheets/d/1IEvv4enfouBXrGEVLTKeQNYTJIzQHmRNyKTxKAxEAfg/edit#gid=0。

英文:

I have a problem with get credentials from Google API.
I searched a lot of old topics and can't find solution.

My code looks like:

static readonly string[] Scopes = { SheetsService.Scope.Spreadsheets };
static readonly string ApplicationName = "Dot Tutorials";
static readonly string sheet = "Arkusz1";
static readonly string SpreadsheetId = "1IEvv4enfouBXrGEVLTKeQNYTJIzQHmRNyKTxKAxEAfg";
static SheetsService service;
GoogleCredential credential;
    //Reading Credentials File...
    using (var stream = new FileStream("secret.json", FileMode.Open, FileAccess.Read))
    {
        credential = GoogleCredential.FromStream(stream)
            .CreateScoped(Scopes);
    }
    // Creating Google Sheets API service...
    service = new SheetsService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName,
    });

Secret .json I downloaded :

{
  "installed": {
    "client_id": "1087609048092-hdh5ku57kaamha72o7t3tdtkb62vq5ur.apps.googleusercontent.com",
    "project_id": "excellent-well-151116",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "*********hidden my secret code*******",
    "redirect_uris": [
      "http://localhost"
    ]
  }
}

And when I run it I still am getting the error:

> System.InvalidOperationException: 'Error creating credential from JSON or JSON parameters. Unrecognized credential type .'

I don't know what is wrong here. I'm trying to follow this tutorial: https://dottutorials.net/google-sheets-read-write-operations-dotnet-core-tutorial/

My goal is to get values from this spreadsheet into a DataTable: https://docs.google.com/spreadsheets/d/1IEvv4enfouBXrGEVLTKeQNYTJIzQHmRNyKTxKAxEAfg/edit#gid=0.

答案1

得分: 1

以下是您想要的内容:

UserCredential credential2;

using (var stream = new System.IO.FileStream(clientSecretsPath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
    credential2 = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.FromStream(stream).Secrets,
        new[] { GmailService.Scope.MailGoogleCom },
        "my@gmail.com", CancellationToken.None, new FileDataStore("LoginCookieStoredHere"));
}

return new GmailService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential2,
    ApplicationName = "Gmail API Example"
});
英文:

Here is what you want:

        UserCredential credential2;

        using (var stream = new System.IO.FileStream(clientSecretsPath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                credential2 = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.FromStream(stream).Secrets,
                    new[] { GmailService.Scope.MailGoogleCom },
                    "my@gmail.com", CancellationToken.None, new FileDataStore("LoginCookieStoredHere"));
            }

            return new GmailService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential2,
                ApplicationName = "Gmail API Example"
            });
        }

huangapple
  • 本文由 发表于 2023年3月7日 19:20:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定