0

我正在尝试使用“googleapis/python-retail”Python 包将产品导入 Recommendations AI 目录。但是当我使用 ProductServiceAsyncClient 执行此操作时,会向我返回一个错误"google.api_core.exceptions.GoogleAPICallError: None Unexpected state: Long-running operation had neither response nor error set."

在“推荐 AI -> 数据 -> 导入活动”中,我没有任何日志。我也没有错误

我尝试使用此包获取手动上传的产品并再次导入以排除格式错误。但它也失败了

我的示例代码:

async def import_products():
    product_client = ProductServiceAsyncClient()

    product_obj = Product(
        id='product-id',
        categories=["Larry", "Steve", "Eric"],
        title='title',
    )

    product_items = [product_obj]
    
    product_inline_source = ProductInlineSource(
        products = product_items
    )

    product_input_config = ProductInputConfig(
        product_inline_source = product_inline_source
    )

    import_products_request = ImportProductsRequest(
        parent="projects/xxxxxxx/locations/global/catalogs/default_catalog/branches/default_branch",
        input_config=product_input_config
    )
    
    import_products = await product_client.import_products(
        request=import_products_request,
        timeout=1000
    )

    return import_products

asyncio.run(import_products())
4

1 回答 1

1

我尝试使用适当的身份验证来实现您的示例代码,并且能够成功地将产品导入 Recommendations AI 产品目录。

根据文档,导入操作也可能仍在进行中,因此长时间运行的操作的“完成”参数未设置为“真”。

实现代码:

import asyncio
from google.cloud import retail
from google.oauth2 import service_account
from google.cloud.retail import ProductServiceAsyncClient, Product, ProductInlineSource, ProductInputConfig, ImportProductsRequest

SERVICE_ACCOUNT_FILE = "path_to_key.json"
PROJECT_NUM = "my-project"

SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
credentials = service_account.Credentials.from_service_account_file(
 SERVICE_ACCOUNT_FILE, scopes=SCOPES)

async def import_products():
   product_client = retail.ProductServiceAsyncClient(credentials=credentials)

   product_obj = Product(
       id='product-id',
       categories=["Larry", "SteveS", "Eric"],
       title='title',
   )

   product_items = [product_obj]
  
   product_inline_source = ProductInlineSource(
       products = product_items
   )

   product_input_config = ProductInputConfig(
       product_inline_source = product_inline_source
   )

   import_products_request = ImportProductsRequest(
       parent="projects/<project-id>/locations/global/catalogs/default_catalog/branches/default_branch",
       input_config=product_input_config
   )
  
   import_products = await product_client.import_products(
       request=import_products_request,
       timeout=1000
   )

   return import_products
asyncio.run(import_products())
于 2021-09-29T12:22:45.483 回答