使用 p12 密钥进行身份验证时出现错误用户无法访问帐户

Authentication with p12 key give error User cannot access account

我有一个 MVC 网络应用程序可以更新电子商务网站上的产品。现在我们注册了 google 商家中心,我的 objective 是同时更新产品。我正在使用 Google.Apis.ShoppingContent.v2_1 API.

这是我的 API 凭证

这是我的 API 服务帐户

我使用了用户的 google 帐户电子邮件地址以及服务帐户电子邮件,但结果相同。

我有以下

    static string[] Scopes = { ShoppingContentService.Scope.Content};
    static string P12Secret =  @"~\Content\XXXXXX-5cab03fb904a.p12";
    static string userName = "serviceaccount@gserviceaccount.com";
    static public async Task RunTest2()
        {
            var certificate = new X509Certificate2(P12Secret, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
            var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(userName)
            {
                Scopes = Scopes
            }.FromCertificate(certificate));

            var service = new ShoppingContentService(new BaseClientService.Initializer
            {
                ApplicationName = ApplicationName,
                HttpClientInitializer = credential
            });

            try
            {
                var result = await service.Products.List("My MerchantID").ExecuteAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

当我执行 var result = await service.Products.List("My MerchantID").ExecuteAsync();我收到错误

e.Message = "Google.Apis.Requests.RequestError\nUser cannot access account 123456789 [401]\r\nErrors [\r\n\tMessage[User cannot access account 123456789 ] Location[ - ] Reason[auth/account_access_denied] Domain[content.ContentErrorDomain]\r\n]\r\n"

Documentaiton

Service accounts are special Google accounts that can be used by applications to access Google APIs programmatically via OAuth 2.0. A service account uses an OAuth 2.0 flow that does not require human authorization. Instead, it uses a key file that only your application can access. This guide discusses how to access the Content API for Shopping with service accounts.

服务帐号需要预授权。如果不是,则它无权访问任何数据。

User cannot access account 123456789

表示它没有访问权限,您忘记授予它访问权限。检查 Documentaiton 查找下面的部分,然后执行所有步骤。