0

We use a remote service (not Drupal) to publish press releases and to show those releases in a remote newsroom.

This remote service offers an API so we should be able to fetch and show the newsroom on our Drupal site. To use the API a custom token must be present in the request header and the method used must be GET:

Notified-Custom-Token: [TOKEN]

This is what I have done so far in a custom module named notified_api. [custom token] is replaced by a unique token genererated for our newsroom and [id] is replaced by our newsroom ID:

function notified_api_preprocess_page(&$variables) {
  // Fetch newsroom from Notified API with custom token
  $headers =  array('Authorization'=>'Notified-Custom-Token [token]', 'Content-Type'=>'application/json' );
  $url = "https://api.newsroom.notified.com/api/newsrooms/[id]";

  $options=array('headers'=>$headers, 'method'=>'GET');

  $result=drupal_http_request($url, $options);

  dpm($result);
}

Partial result from the dpm:

... (Object) stdClass
url (String, 53 characters ) https://api.newsroom.notified.com/api/newsrooms...
status (String, 5 characters ) Done.
code (Integer) 401
chunk_size (Integer) 32768
data (String, 0 characters )
request (String, 438 characters ) GET /api/newsrooms/345345 HTTP/1.0 Authorizatio...
GET /api/newsrooms/345345 HTTP/1.0
Authorization: Notified-Custom-Token 9832502938403876504329820348ewieitrje...
Content-Type: application/json
User-Agent: Drupal (+http://drupal.org/)
Connection: close
Host: api.newsroom.notified.com

options (Array, 20 elements)
headers (Array, 5 elements)
Authorization (String, 258 characters ) Notified-Custom-Token 9832502938403876504329820348ewieitrje...
Content-Type (String, 16 characters ) application/json
User-Agent (String, 28 characters ) Drupal (+http://drupal.org/)
Connection (String, 5 characters ) close
Host (String, 25 characters ) api.newsroom.notified.com
method (String, 3 characters ) GET

What I get is a 401 message: unauthorized. Can anyone see what I am doing wrong?

1 Answer 1

0

In case anyone else have the same problem. By replacing the name "Authorization" like this:

  $headers =  array('Notified-Custom-Token'=>'my-unique-token', 'Content-Type'=>'text/json' );

... it worked and I got a success response with code 200. My mistake was to believe that Authorization was a mandatory name in the name/value pair.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.