【问题标题】:google service account authentication with json file in .net 4.0使用 .net 4.0 中的 json 文件进行谷歌服务帐户身份验证
【发布时间】:2018-01-24 05:46:34
【问题描述】:

需要使用 OAuth 对 google 服务帐户进行身份验证。 我有 json 格式的谷歌凭据文件。 裁判Google Service Account Authentication with Json file

能够创建 RASParameters 但不知道如何进一步进行。 我对 google apis 实现非常陌生。 找到了 .p12 文件的各种示例,但没有找到带有 .json 文件的示例。 而且我正在使用 .net 4.0 框架,因此无法使用 .net 4.5+ 框架提供的 googlecredential 类。

【问题讨论】:

    标签: c# json google-oauth


    【解决方案1】:

    我认为您需要创建一个 JsonCredentialParameters 与您拥有的那个 Json 文件。此类位于 Google.Apis.Auth.OAuth2 命名空间下,因此您只需要拥有适当的 Google Apis Auth 包即可。

    然后您可以使用正确的用户帐户和范围创建新的 ServiceAccountCredential。

    protected override ServiceAccountCredential GetCredential()
        {
            var credentialParameters = GetCredentialParameters();
            return new ServiceAccountCredential(
               new ServiceAccountCredential.Initializer(credentialParameters.ClientEmail)
               {
                   Scopes = Scopes,
                   User = User
               }.FromPrivateKey(credentialParameters.PrivateKey));
        }
        JsonCredentialParameters GetCredentialParameters()
        {
            return JsonSerializer.Deserialize<JsonCredentialParameters>(new FileStream(File, FileMode.Open));
        }
    

    【讨论】:

    • 我已尝试安装 Google.Apis.Auth.OAuth2 包,但它不适用于 .net 4.0。所以我已经安装了 Google.Apis.Authentication.OAuth2
    猜你喜欢
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 2020-05-21
    相关资源
    最近更新 更多