C# Google Drive API v3使用JSON而不是P12(服务帐户)-意外字符

C# Google Drive API v3使用JSON而不是P12(服务帐户)-意外字符,c#,google-api,google-drive-api,google-api-dotnet-client,service-accounts,C#,Google Api,Google Drive Api,Google Api Dotnet Client,Service Accounts,错误:分析值e时遇到意外字符。路径 '',第0行,第0位 我正在使用Google.Net客户端库访问googledriveapi v3,特别是包。我授权使用C#的“服务帐户” 使用p12密钥进行授权没有问题。但是,建议使用JSON,并保留p12格式以实现向后兼容性 我从Google开发者控制台下载了JSON文件,并尝试使用以下代码进行授权: public static Google.Apis.Drive.v3.DriveService AuthenticateServiceAccount

错误:分析值e时遇到意外字符。路径 '',第0行,第0位

我正在使用Google.Net客户端库访问googledriveapi v3,特别是包。我授权使用C#的“服务帐户”

使用p12密钥进行授权没有问题。但是,建议使用JSON,并保留p12格式以实现向后兼容性

我从Google开发者控制台下载了JSON文件,并尝试使用以下代码进行授权:

    public static Google.Apis.Drive.v3.DriveService AuthenticateServiceAccountJSON(string keyFilePath) {

        // check the file exists
        if (!File.Exists(keyFilePath)) {
            Console.WriteLine("An Error occurred - Key file does not exist");
            return null;
        }

        string[] scopes = new string[] { DriveService.Scope.Drive,                  // view and manage your files and documents
                                         DriveService.Scope.DriveAppdata,           // view and manage its own configuration data
                                         DriveService.Scope.DriveFile,              // view and manage files created by this app
                                         DriveService.Scope.DriveMetadataReadonly,  // view metadata for files
                                         DriveService.Scope.DriveReadonly,          // view files and documents on your drive
                                         DriveService.Scope.DriveScripts };         // modify your app scripts     

        try {
            using (var stream = new FileStream(keyFilePath, FileMode.Open, FileAccess.Read)) {
                var credential = GoogleCredential.FromStream(stream);
                if (credential.IsCreateScopedRequired) {
                    credential.CreateScoped(scopes);
                }
                // Create the service.
                Google.Apis.Drive.v3.DriveService service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer() {
                    HttpClientInitializer = credential,
                    ApplicationName = "MyDrive",
                });
                return service;
            }
        } catch (Exception ex) {
            Console.WriteLine(ex.InnerException);
            return null;

        }
    }
我在记事本中查看了JSON文件,它似乎是加密的

“EWOGICJ0EXBLIJOGINNLCNZPY2VFYWNJB3VUDCIAGINB2PLY3RFAWQIAICMFAWFUDC1TZXJJDXJ5LTEYMJKWNYISIAGIN……”


是否可以继续使用P12?

确保您正在下载正确的文件

GoogleCredential.FromStream(stream)
使用JSON文件。它应该是这样的:

{
  "type": "service_account",
  "project_id": "",
  "private_key_id": "",
  "private_key": "-----BEGIN PRIVATE KEY-----
---END PRIVATE KEY-----\n",
  "client_email": "",
  "client_id": "",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": ""
}

通过单击显示客户端ID的网格右侧的下载JSON按钮,您可以在获取此文件。只需确保所选ID的类型为“服务帐户客户端”。

这对我来说可以使用来自Google开发者控制台的JSON凭据文件。我正在使用Analytics服务,但只需更换驱动器服务的相应名称:

private AnalyticsReportingService service;

public async Task GetAuthorizationByServiceAccount()
    {
        string[] scopes = new string[] { AnalyticsReportingService.Scope.AnalyticsReadonly }; // Put your scopes here
        var keyFilePath = AppContext.BaseDirectory + @"KeyFile.json";

        //Console.WriteLine("Key File: " + keyFilePath);

        var stream = new FileStream(keyFilePath, FileMode.Open, FileAccess.Read);

        var credential = GoogleCredential.FromStream(stream);
        credential = credential.CreateScoped(scopes);

        service = new AnalyticsReportingService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "<Your App Name here>",
        });
    }
私有分析报告服务;
公共异步任务GetAuthorizationByServiceAccount()
{
string[]scopes=新字符串[]{AnalyticsReportingService.Scope.AnalyticsReadonly};//将范围放在这里
var keyFilePath=AppContext.BaseDirectory+@“KeyFile.json”;
//Console.WriteLine(“密钥文件:”+keyFilePath);
var stream=新文件流(keyFilePath,FileMode.Open,FileAccess.Read);
var-credential=GoogleCredential.FromStream(流);
凭证=凭证。创建作用域(作用域);
服务=新的AnalyticsReportingService(新的BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=“”,
});
}

我使用p12文件时,他们都应该亲自工作。这应该有帮助,谢谢,我要用p12。如果有人能说一些关于JSON文件或者它应该如何工作的话,请告诉我。这很有帮助,谢谢。我最后把json文件放在了一个资源中,但上面的内容让我开始了。你知道我如何提取令牌,以便构建服务器端授权并构建google图表吗?