【问题标题】:how use Google OAuth2 using ServiceAccount in .net?如何在 .net 中使用 ServiceAccount 使用 Google OAuth2?
【发布时间】:2019-12-26 03:40:54
【问题描述】:

是否有任何示例如何使用 .net 中的服务帐户访问 google 服务 API?

private const string SERVICE_ACCOUNT_EMAIL = "xxxxxxxxxxx@developer.gserviceaccount.com";
private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"\path\test-privatekey.p12";

static DriveService BuildService() 
{
    X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret",
    X509KeyStorageFlags.Exportable);

    var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
    {
        ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
        Scope = DriveService.Scopes.Drive.GetStringValue(),
    };
    var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);

    return new DriveService((new BaseClientService.Initializer()
    {
        Authenticator = auth
    });
}

返回 OAuth 连接失败。如何做到这一点?

【问题讨论】:

  • 您找到解决方案了吗?

标签: c# google-api google-oauth


【解决方案1】:

这个案例在我的网站上工作

var certificate = new X509Certificate2("pathTo***.p12", "notasecret", X509KeyStorageFlags.Exportable);
        var serviceAccountEmail = "********-*********@developer.gserviceaccount.com";
        var userAccountEmail = "******@gmail.com";
        ServiceAccountCredential credential = new ServiceAccountCredential(
                   new ServiceAccountCredential.Initializer(serviceAccountEmail)
                   {
                       Scopes = new[] { DriveService.Scope.Drive },
                       User = userAccountEmail

                   }.FromCertificate(certificate));

        // Create the service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "*****",
        });

【讨论】:

  • 我遇到了奇怪的行为:每当我输入绝对路径时,我总是得到异常:“系统找不到指定的文件。|| 路径:C:\www\myproject\App_Data\ key.p12",你我把这个文件放在 App_Data 文件夹中并标记为'总是复制'
【解决方案2】:
  1. 创建服务帐户密钥凭据
  2. 为服务创建私钥。 (密钥 json)。 示例:
    {
      "type": "service_account",
      "project_id": "...",
      "private_key_id": "....",
      "private_key": "....",
      "client_email": ".....@developer.gserviceaccount.com",
      "client_id": "....",
      "auth_uri": "...accounts.google.com/o/oauth2/auth",
      "token_uri": "...accounts.google.com/o/oauth2/token",
      "auth_provider_x509_cert_url": "...www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "...www.googleapis.com/robot/v1/metadata/x509/....-compute%40developer.gserviceaccount.com"
    }
  1. 使用此 json,您必须生成一个 c# 类。您可以使用 (http://json2csharp.com/)。这更快
  2. 使用此代码生成凭证:
       var _pathJson = @"C:\servicekey.json";
       var json = File.ReadAllText(_pathJson);
       var cr = JsonConvert.DeserializeObject<PersonalServiceAccountCred>(json); 
       // "personal" service account credential
       // Create an explicit ServiceAccountCredential credential
       var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.ClientEmail)
                {
                    Scopes = new[] { YouTubeService.Scope.YoutubeUpload /*Here put scope that you want use*/}
                }.FromPrivateKey(cr.PrivateKey));

【讨论】:

    猜你喜欢
    • 2012-01-23
    • 2011-08-12
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    相关资源
    最近更新 更多