【问题标题】:APPLE NEWS API POST RequestAPPLE NEWS API POST 请求
【发布时间】:2023-03-30 05:09:01
【问题描述】:

我正在尝试使用苹果新闻 API 发布文章,尝试使用邮递员按照文档提供的所有步骤,以及执行苹果文档中提供的 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 的批准等。任何信息都会有所帮助。

【问题讨论】:

  • 您是否有意遗漏了channel_idapi_key_idapi_key_secret 的值?因为如果您在发布时使用上面的脚本,它将无法工作,因为您无法进行身份验证。这也符合您收到的错误消息:Wrong signature
  • 是的,故意遗漏了这些值。

标签: python python-requests apple-news


【解决方案1】:

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

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

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

执行python文件时,应该是这样执行的:

python script_name.py 'folder/'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2018-08-04
    • 2020-03-06
    • 2018-08-08
    相关资源
    最近更新 更多