我正在尝试发布文章使用苹果新闻API尝试使用Postman遵循文档提供的所有步骤,以及执行Apple文档中提供的python代码。
POSThttps://news-api.apple.com/channels/channelID/articles2018-02-06T05:15:53Zmultipart/form-data;
Authorization headers authorization = {str} 'HHMAC; key=ID; signature=ID; date=2018-02-07T05:15:53Z'
得到:错误陈述错误的单曲
Python
import requests
import base64
from hashlib import sha256
import hmac
from datetime import datetime
import glob
import argparse
import os
import mimetypes
from requests.packages.urllib3.filepost import encode_multipart_formdata
from requests.packages.urllib3.fields import RequestField
arg_parser = argparse.ArgumentParser(description='Publish an article using the Apple News API')
arg_parser.add_argument('article_directory', metavar='ARTICLE_DIR', type=str, help='A directory containing an article.JSON file and resources')
args = arg_parser.parse_args()
channel_id = '[YOUR CHANNEL-ID]'
api_key_id = '[YOUR API-KEY]'
api_key_secret = '[YOUR API KEY-SECRET]'
method = 'POST'
url = 'https://news-api.apple.com/channels/%s/articles' % channel_id
session = requests.Session()
session.verify = False
def part(filename):
name = os.path.basename(filename)
with open(filename) as f:
data = f.read()
part = RequestField(name, data)
part.headers['Content-Disposition'] = 'form-data; filename="%s"; size=%d' % (name, os.stat(filename).st_size)
part.headers['Content-Type'] = 'application/JSON' if name.endswith('.JSON') else 'application/octet-stream'
return part
def send_signed_request(method, url, filenames):
body, content_type = encode_multipart_formdata([part(f) for f in filenames])
req = requests.Request(method, url, data=body, headers={'Content-Type': content_type})
req = req.prepare()
date = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
canonical_request = method + url + str(date) + content_type + body
key = base64.b64decode(api_key_secret)
hashed = hmac.new(key, canonical_request, sha256)
signature = hashed.digest().encode('base64').rstrip('/n')
authorization = 'HHMAC; key=%s; signature=%s; date=%s' % (api_key_id, str(signature), date)
req.headers['Authorization'] = authorization
return session.send(req)
response = send_signed_request(method, url, glob.glob('%s/*' % args.article_directory))
print response.status_code
print response.text
错误:{"错误":[{"代码":" MISSING"," keyPath":["文章上传.json"]}]}
我还将python代码转换为java并执行了相同的错误,但能够阅读文章。
问题: 对于出现问题的任何建议或者为了创建文章,苹果公司都会批准苹果帐户等。任何信息都会有所帮助。
答案 0 :(得分:0)
此脚本需要文件夹路径作为参数。此文件夹必须具有以下结构:
--- folder
|---- article.json
|---- image.jpg
如果您的article.json文件需要图像文件,则它们是可选的。
执行python文件时,应按以下方式执行:
python script_name.py 'folder/'