2

I am able to connect with the help of apple news api but I can't able to send data in json format. My code is below. but when I try to attach json file. I am getting signature failure message.

    <?php
$channel_id = 'xxxxxxxxxxxxxxxxxx';
$api_key_id = 'xxxxxxxxxxxxxxx';
$api_key_secret = 'xxxxxxxxxxxxxxxx';
// use the correct values above    
$data = file_get_contents('article.json');
$Content_type="application/json";

$url = 'https://news-api.apple.com/channels/'.$channel_id;
$date = gmdate('Y-m-d\TH:i:s\Z');
$canonical_request = 'GET'.$url.$date.$Content_type;
$key = base64_decode($api_key_secret);
$hashed = hash_hmac('sha256', $canonical_request,$key,true);
$signature = base64_encode($hashed);
echo $signature;
//curl options
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);

$headers = array();
$headers[] = "Authorization: HHMAC; key={$api_key_id}; signature={$signature}; date={$date}";

$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_POST, true);

//curl_setopt( $ch, CURLOPT_POSTFIELDS, $data);

//get result
$server_output = curl_exec ($ch);
curl_close ($ch);

print_r(json_decode($server_output));

?>
deceze
  • 510,633
  • 85
  • 743
  • 889
anu g prem
  • 545
  • 5
  • 15

1 Answers1

1

Since you are sending a GET request, you are incorrectly including the content type when building the canonical request.

This should work for you, then:

$canonical_request = 'GET'.$url.$date;

For reference, see Apple News API Reference: Setting Up MAC/HMAC Authentication:

  1. Create a canonical version of the request as a byte-wise concatenation of the following:

    • The HTTP method (for example, GET or POST, in all caps)
    • The full URL of the request
    • The current date in ISO 8601 format
  2. If the request is a POST request and it includes an entity, include the following:

    • The value of the Content-Type header
    • The full content of the entity
localheinz
  • 9,179
  • 2
  • 33
  • 44