如何使用 .pem 文件向 .net 中的 Google 购物 API 发出获取请求

How do I use a .pem file to make get requests to the Google Shopping API in .net

我的任务是从商家中心帐户中检索数据源。我已经获得了一个 .pem 文件和一些用于在 JavaScript 中发出请求的示例代码...但是我被要求提供一个 C# 解决方案并且我正在努力。

我特别想向https://www.googleapis.com/content/V2/[merchantId]/datafeedstatuses提出请求

我有 merchantId 并且了解范围。我想我从概念上理解 oauth 2.0 请求是如何工作的(尽管我之前已经发出了 oauth 2.0 请求)......但是我找不到任何示例代码来说明如何使用 pem 文件来发出这些请求之一。

如果有人能给我一个起点,我将不胜感激。

我从未使用过 Google 的购物 API。我处理电子邮件、目录、人员 api。这不简单。我查了 Google 有 nuget 包供他们 API 购物。您可能需要的是 https://www.nuget.org/packages/Google.Apis.ShoppingContent.v2/。这是我的一些代码,您可以根据需要采用这些代码:

var initializer = new ServiceAccountCredential.Initializer(GoogleSettings.ClientEmail)
{
    Scopes = GoogleSettings.Scopes,
    User = GoogleSettings.AdminAccount,
};
var initFromCert = initializer.FromPrivateKey(GoogleSettings.PrivateKey);
var credential = new ServiceAccountCredential(initFromCert);

然后我使用了目录服务

var directoryService = new DirectoryService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = GoogleSettings.ProjectID,
            });

所有 GoogleSettings 都是从 JSON 下载的 Google 项目设置中收集的。在你的情况下,pem 文件应该包含可以提取的私钥。否则你需要得到它。