6

UPDATE: Below code works for me. Hope it helps someone to figure out their problem.

After having several errors, it helped going back and looking at all possible error codes on Apple News Developer site. Look at specific error numbers in your code and decide what might be wrong with it.

Follow the examples on the Apple News Developer site. Even though they are vague they do contain crucial information!

//set the timezone
date_default_timezone_set('UTC');

//get json to be sent

$raw = file_get_contents('article.json');
$eol = "\r\n";
$data = '';
$bound= '535e329ca936f79a19ac9a251f7d48f7';

$data='--'.$bound.$eol.
"Content-Type: application/json" . $eol.
"Content-Disposition: form-data; name=metadata" . $eol. $eol.
'{
"data": {
    "isCandidateToBeFeatured": "false",
    "isSponsored": false,
    "isPreview": true     
}
}' .$eol.
'--'.$bound.$eol.
"Content-Type: application/json" . $eol.
"Content-Disposition: form-data; filename=article.json; name=article.json".$eol.$eol.
$raw.$eol.
'--'.$bound.'--'.$eol.$eol;

//set variables
$http_method = 'POST';
$date = gmdate('Y-m-d\TH:i:s\Z');
$key = 'xxx';
$url = 'https://news-api.apple.com/channels/xxx/articles';
$secret = 'xxx';

//cannonical request
$canonical_request = $http_method . $url . $date. 'multipart/form-data; boundary=535e329ca936f79a19ac9a251f7d48f7' . $data;

//Signature
$secretKey = base64_decode($secret);
$hash = hash_hmac('sha256', $canonical_request, $secretKey, true);
$signature = base64_encode($hash);

$authHeader = "HHMAC; key=$key; signature=$signature; date=$date;";
$headers = array();

$headers[] = "Authorization: $authHeader";
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: multipart/form-data; boundary=535e329ca936f79a19ac9a251f7d48f7";
$headers[] = "Content-Length: ".strlen($data); 

//curl options
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//get result
$server_output = curl_exec ($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $status;

curl_close ($ch);
print_r(json_decode($server_output));
Chulsy
  • 31
  • 8
Erhan
  • 134
  • 8
  • Where do you get $bound from? Is it just arbitrary 32 bit hash? – esod Jun 24 '19 at 16:36
  • Dont really remember now, but try the same hash and see if it works for you. make sure to add it to your header as well – Erhan Jun 25 '19 at 17:36
  • Thank you so much for sharing this. I was having such a hard time getting all of the headers right to create a new article, this worked first try. :) – MyNotes Aug 05 '22 at 09:09

2 Answers2

2

Your content type header should be Content-Type: application/json but the content type for authorization is just the value `application/json'. Try to use that in the canonical request instead of the full header.

Chulsy
  • 31
  • 8
ChristianM
  • 1,793
  • 12
  • 23
  • YES, thank you at least now I got a different error message I need to dig through. Basic mistakes but easy to miss. Thanks again. I updated the code to true form – Erhan Mar 07 '18 at 15:15
  • Nice catch @ChristianM! :) – activout.se Mar 07 '18 at 17:48
  • I still cant figure out the next step though. I need to look with fresh set of eyes tomorrow. Where I am at right now I am either getting INVALID_MIME_MULTIPART or MISSING [keyPath] => Array ( [0] => article.json . I know JSON file is there but can't yet figure it out what is missing. – Erhan Mar 07 '18 at 20:03
1

Your Content-type header is missing, add it like this:

$headers[] = $Content_Type;
activout.se
  • 6,058
  • 4
  • 27
  • 37
  • Unfortunately it did not work still getting missing signature error. I updated my code above with the latest version with your comment. – Erhan Mar 07 '18 at 13:45