如何使用服务器密钥在Google Spreadsheet Api V4中进行身份验证

时间:2016-08-09 15:56:17

标签: c# google-sheets-api

我正在尝试使用带有服务器密钥的google电子表格(在console.cloud.google.com中创建)

var service = new SheetsService(new BaseClientService.Initializer
    {
        ApplicationName = "myappname",
        ApiKey = "mykey"
    });

但在第一次请求后我收到错误

Message[The request does not have valid authentication credentials.]

那么如何仅使用服务器密钥进行身份验证?

可能最好使用服务帐户凭据来处理来自服务器的api(如果可能的话)?

1 个答案:

答案 0 :(得分:0)

试试这个code written by Владимир Рак

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