Google BigQuery与C#示例示例

时间:2014-04-10 19:40:17

标签: c# google-oauth google-bigquery google-api-dotnet-client

我需要使用Google BigQuery API查询数据,并使用服务帐户进行调用。

但是我很难找到.NET Samples,而且二进制文件中没有包含任何文档(Google.Apis.Bigquery.dll)。任何人都可以为我提供C#.NET的示例用法吗?

1 个答案:

答案 0 :(得分:3)

此信息来自:Google APIs Client Library for .NET

该示例显示了如何在Google+ api中使用服务帐户。我编辑了一点用于BigQuery。

using System;
using Google.Apis.Auth.OAuth2;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Bigquery.v2;
using Google.Apis.Services;

//Install-Package Google.Apis.Bigquery.v2
namespace GoogleBigQueryServiceAccount
{
class Program
{
    private static String ACTIVITY_ID = "z12gtjhq3qn2xxl2o224exwiqruvtda0i";
    static void Main(string[] args)
    {

        Console.WriteLine("BigQuery API - Service Account");
        Console.WriteLine("==========================");

        String serviceAccountEmail = "SERVICE_ACCOUNT_EMAIL_HERE";

        var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer(serviceAccountEmail)
           {
               Scopes = new[] { BigqueryService.Scope.DevstorageReadOnly }
           }.FromCertificate(certificate));

        // Create the service.
        var service = new BigqueryService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "BigQuery API Sample",
        });

        //Note: all your requests will run against Service.

    }
}

}