【问题标题】:Google Maps Engine Service Account with Google APIs .NET使用 Google API .NET 的 Google Maps Engine 服务帐户
【发布时间】:2014-03-10 21:57:23
【问题描述】:

我正在开发一项服务,该服务需要授权服务器到 Google 地图引擎的服务器。

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,但是使用证书。

有人可以帮我吗?

亚历克斯

【问题讨论】:

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


    【解决方案1】:

    我会建议您执行以下操作:

    1. 首先使用Microsoft.Net.Http 包。 Google.Apis 依赖它,所以你已经安装好了。

    2. 尝试使用以下代码:

    // 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");
    

    我没有测试它,但它应该为你创造奇迹:)

    【讨论】:

    • 太棒了!我还用 POST 对其进行了测试,并且工作正常。非常感谢@peleyal!
    猜你喜欢
    • 1970-01-01
    • 2014-06-23
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 2021-09-30
    • 2019-11-06
    • 1970-01-01
    相关资源
    最近更新 更多