使用Google API .NET的Google Maps Engine服务帐户

时间:2014-03-09 02:51:52

标签: google-oauth x509certificate google-api-dotnet-client google-maps-engine

我正在开发一项服务,要求授权服务器向Google Maps Engine提供服务。

https://developers.google.com/maps-engine/documentation/oauth/serviceaccounthttps://developers.google.com/api-client-library/dotnet/guide/aaa_oauth?hl=en#service_account解释了如何执行此操作。

但是,在Google API中。 NET尚未拥有Google Maps Engine API的服务等级,那么我想参与一下图书馆的流程,因为我不想做所有管理JWT的艰苦工作。

我认为:

var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
        Scopes = new[] { "https://www.googleapis.com/auth/mapsengine" }
    }.FromCertificate(certificate));

var request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/mapsengine/v1/projects");
credential.ApplyAuthenticationToRequest(request);

我想使用纯HttpWebRequest,但是使用证书。

有人可以帮助我吗?

亚历

1 个答案:

答案 0 :(得分:1)

我建议你做以下事情:

  1. 首先使用Microsoft.Net.Http包。 Google.Apis依赖于它,因此您已安装它。

  2. 尝试使用以下代码:

  3. // Create HTTP client using ConfigurableMessageHandler.
    // More details available in:
    http://contrib.google-api-dotnet-client.googlecode.com/hg/1.8.0-rc/documentation/classGoogle_1_1Apis_1_1Http_1_1ConfigurableMessageHandler.html.
    var httpClient = new HttpClientFactory().CreateHttpClient(new CreateHttpClientArgs
        {
            ApplicationName = "YOUR_APP_NAME",
        });
    // Add your credential as interceptor.
    httpClient.MessageHandler.ExecuteInterceptors.Add(credential);
    // Create the request.
    var response = await httpClient.GetAsync(
        "https://www.googleapis.com/mapsengine/v1/projects");
    

    我没有测试它,但它应该为你做魔术:)