我正在尝试使用Python在fetchapp API(REST API)上发布新产品。但是,我一直在阅读API文档并尝试解决它,但我一直收到404错误。这就是API文档所说的,这就是我所拥有的。
API doc:
All requests require basic authentication. To make a proper request,
you must set the authorization header of your request and supply
the generated string. The string is your personal key and token with
a colon ("demokey:demotoken") converted to a base-64 string. For
example...
"demokey:demotoken".to_base64 is ZGVtb2tleTpkZW1vdG9rZW4=
Then, place the string into the authorization header of your HTTP requests...
GET /api/v2/orders HTTP/1.1
Host: app.fetchapp.com
Authorization: Basic ZGVtb2tleTpkZW1vdG9rZW4=
Content-Type: application/xml
Please make sure to set the Content-Type header to 'application/xml'.
POST to create the specified product...
/api/v2/products
/api/v2/products/create
/api/v2/products/create.xml
For example...
<?xml version="1.0" encoding="UTF-8"?>
<product>
<sku>1</sku>
<name>Carrot Juice</name>
<price type="float">10.00</price>
<files type="array">
<id>4443443</id>
<id>4443444</id>
</files>
</product>
我有什么:
from requests_oauthlib import OAuth1Session
import requests
from requests_oauthlib import OAuth1
import json
from oauthlib.oauth1 import SIGNATURE_TYPE_QUERY, SIGNATURE_TYPE_BODY
from requests_toolbelt import MultipartEncoder
import time
from requests.auth import HTTPBasicAuth
url = 'http://www.fetchapp.com/api/v2/products/create.xml'
headers = {"Accept": "application/xml"}
auth=HTTPBasicAuth('demokey', 'demotoken')
data = {'sku': "CJ0001", 'name': "Carrot Juice", 'price type="float"': "1.00"}
r = requests.post(url, auth=auth, headers=headers, data=data, verify=True)
print r#.text