0

I am trying to post articles using apple news API tried using the Postman following all the steps provided by documentation, as well executed the python code provided in apple documentation.

    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'

got: error stating Wrong singature

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

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

I also converted the python code to java and executed seeing the same error but able to read articles.

Question: Any suggestions on what is going wrong or In order to create articles does apple account be approved by Apple etc. Any information would be helpfull.

Benjy Malca
  • 597
  • 1
  • 9
  • 21
sunil
  • 1
  • Did you intentionally leave out values for `channel_id`, `api_key_id` and `api_key_secret`? Because if you use the script above as you posted it, it won't work because you can not authenticate. This would also fit the error message you got: `Wrong signature`. – Jens Feb 08 '18 at 13:13
  • Yes, intentionally left out the values. – sunil Feb 08 '18 at 17:16

1 Answers1

0

This script requires a folder path as an argument. This folder has to have the following structure:

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

Image files are optional if your article.json file requires them.

When the python file is executed, it should be executed like this :

python script_name.py 'folder/'
Benjy Malca
  • 597
  • 1
  • 9
  • 21