NASA/ADS changes its API

Now that the NASA Abstract Data Service (ADS) has changed its API without offering a legacy service, a number of things I deal with broke. While I understand wanting to make progress, one should always strive to break as few things as possible, and since all the URLs have changed in the new system, I don’t see why keeping the old ones would have been that much of a problem. Oh well, onwards and upwards.

If you used the NASA/ADS WordPress plugin to display your private libraries (as I do for my publications list), that’s one of the things that broke. But it’s not too hard to fix. All you need to do is edit the following file in your wordpress folder: plugins/wp-nasaads-query-importer/wp-nasaads-query-importer.php.

You want to replace the function wp_nasaads_query_importer_get_xml() with the code block below. Also, in order to access ADS’s API, you need to create an account on the new system (or migrate your old account) and then generate a key. Put this key in code, replacing <insert key here>. Yes, I know, but this is a quick hack after all.

function wp_nasaads_query_importer_get_xml($query_url)
{
   //I format the query to ADS
   $query_url = wp_nasaads_query_importer_buld_ads_url($query_url);

   // Authentication is now done through an API key
   $token = "<insert key here>";
   $token = "Bearer " . $token;

   $bibcodes = wp_remote_get(html_entity_decode($query_url),
   array(
      "user-agent"=>"Mozilla/5.0 (WP NASA/ADS Query Importer - WordPress Plugin)",
      "timeout"=>60,
      "headers"=>array("Authorization"=>$token)));

   //and I check that the request was successful (if I get XML I was)
   if (! is_array($bibcodes) )
      return FALSE;

   // The return value is JSON format, so decode that and get the "documents"
   $bibcodes = json_decode($bibcodes["body"]);
   $bibcodes = $bibcodes->documents;

   // Now we need to export as XML, which is another call. Use POST and it 
   // needs list of bibcodes in JSON format
   $urlbase = "https://api.adsabs.harvard.edu/v1/export/refxml";
   $body = json_encode(array("bibcode"=>$bibcodes));

   $response = wp_remote_post(html_entity_decode($urlbase),
      array(
         "user-agent"=>"Mozilla/5.0 (WP NASA/ADS Query Importer - WordPress Plugin)",
         "timeout"=>60,
         "headers"=>array(
            "Authorization"=>$token,
            "Content-Type"=>"application/json",
            "Content-Length"=>strlen($body)
         ),
         "body"=>$body
      )
   );
   if ( is_wp_error($response) )
      return FALSE;
   else
      return json_decode($response['body'])->export;
}
This entry was posted in Uncategorized. Bookmark the permalink.

Comments are closed.