0

我正在尝试使用苹果新闻 API 发布文章,尝试使用 Postman 遵循文档提供的所有步骤,以及执行苹果文档中提供的 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

错误:{"errors":[{"code":"MISSING","keyPath":["article.json"]}]}

我还将 python 代码转换为 java 并执行,看到相同的错误但能够阅读文章。

问题:关于出了什么问题的任何建议或为了创建文章,苹果帐户是否得到了 Apple 等的批准。任何信息都会有所帮助。

4

1 回答 1

0

此脚本需要文件夹路径作为参数。此文件夹必须具有以下结构:

--- folder
 |---- article.json
 |---- image.jpg

如果您的 article.json 文件需要图像文件,则它们是可选的。

当python文件被执行时,它应该像这样执行:

python script_name.py 'folder/'
于 2019-03-20T20:19:13.380 回答