使用 Ebay API 和 Python 销售商品时出现错误 ID 2004

问题描述 投票:0回答:2

我正在尝试使用 eBay API 通过 python 销售商品,但是我不断收到此错误

{"errors":[{"errorId":2004,"domain":"ACCESS","category":"REQUEST","message":"Invalid request","longMessage":"The request has errors. For help, see the documentation for this API."}]}

我不知道为什么它会给我这个,并且用谷歌搜索没有任何效果。

我尚未达到 5000 限制,但有有效的访问代码。

这是我用于 API 的链接 https://api.ebay.com/sell/inventory/v1/inventory_item/

当我使用 API Explorer (https://developer.ebay.com/DevZone/build-test/test-tool/default.aspx?index=0&api=inventory&call=inventory_item-sku__PUT&variation=json&env=product) 时,我得到同样的错误,但它说

Can not instantiate value of type

{
  "errors": [
    {
      "errorId": 2004,
      "domain": "ACCESS",
      "category": "REQUEST",
      "message": "Invalid request",
      "longMessage": "The request has errors. For help, see the documentation for this API.",
      "parameters": [
        {
          "name": "reason",
          "value": "Can not instantiate value of type [simple type, class com.ebay.raptor.slrinvapi.app.entities.InventoryItem] from JSON String; no single-String constructor/factory method"
        }
      ]
    }
  ]
}

这是代码,但没有客户端 ID 和客户端密钥:

import requests
import json
import base64

client_id = '#################################'
client_secret = '#################################'
redirect_uri = 'https://amazon.adamkhattab.co.uk/callback.php'

auth_url = 'https://auth.ebay.com/oauth2/authorize'
token_url = 'https://api.ebay.com/identity/v1/oauth2/token'
api_url = 'https://api.ebay.com/sell/inventory/v1/inventory_item'

def get_access_token(client_id, client_secret, redirect_uri):
    auth_code = input('Please enter the authorization code: ')
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': f'Basic {base64.b64encode(f"{client_id}:{client_secret}".encode()).decode()}'
    }
    data = {
        'grant_type': 'authorization_code',
        'code': auth_code,
        'redirect_uri': redirect_uri
    }
    response = requests.post(token_url, headers=headers, data=data)
    access_token = json.loads(response.text)['access_token']
    return access_token

def list_item(access_token):
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }

    data = {
        "title": "Bluetooth Speaker",
        "condition": "NEW",
        "description": "This is a very good speaker and sounds amazing",
        "imageUrls": [
            "https://amazon.adamkhattab.co.uk/photo.jpg"
        ],
        "price": {
            "value": "20.00",
            "currency": "GBP"
        },
        "quantity": 1
    }

    response = requests.post(api_url, headers=headers, data=json.dumps(data))
    print(response.text)

auth_url += f'?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope=https://api.ebay.com/oauth/api_scope/sell.inventory'

print(f'Please visit the following URL and authorize the application:\n{auth_url}')
access_token = get_access_token(client_id, client_secret, redirect_uri)
list_item(access_token)
python json url oauth-2.0 ebay-api
2个回答
0
投票

我想通了: 我没有在链接中使用 SKU 参数,导致错误。 这是我使用的新链接,没有错误https://api.ebay.com/sell/inventory/v1/inventory_item/1


0
投票

您到底在哪一部分更新了链接。我已经尝试在 api_url 变量上更新它,并且

auth_url += f'?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope=https://api.ebay.com/oauth/api_scope/sell.inventory'
并且似乎在其中任何一个中都不起作用

© www.soinside.com 2019 - 2024. All rights reserved.