Somewhere to keep those API keys: google apps script rest library entry

In the builtwith.com api rest library entry for Excel I covered how to get data about a given web site using the builtwith.com API. I went against my usual rule here, by including an api that needs an API Key. Here’s how to securely store your API keys in one place, and use them without exposing them in your code. This is a subset of the topic, using scriptDB as a lockbox, and before it will work, you would need to follow the instructions there for publishing a scriptDB lockbox.

Assuming you’ve done that, here is the Google Apps Script code for getting your api key out, and running the rest query. You would just need to substitute mcpher.getMyStuff() with yourlibraryname.getMyStuff().

function getApiKey(api) {
  // my private store
  var myKey = mcpher.getMyStuff(api, myStuff.myStuffDb());
  if (myKey.status.code == “good”) {
      return myKey.result.myStuff.consumerKey;
  }
  else {
    mcpher.MsgBox(“unable to find your key for ” + api);
    return null;
  }
}
function testBuiltWith() {
  // get key from my private store
  var myKey = getApiKey(“builtwithapi”);
  if (myKey) {
     mcpher.generalQuery (“builtwith”, “builtwith”,
         mcpher.URLEncode(mcpher.InputBox(“Enter web site name”)),
         undefined, undefined , myKey);
  }
}

You would of course had to have stored your own api key once off, with a function like this (again You substituting mcpher.getMyStuff() with yourlibraryname.getMyStuff() ) .

function createBuiltWithApiKey(){
  return mcpher.createStuff (“builtwithapi”,  myStuff.myStuffDb(),
     {
      consumerKey: ‘whatever your api key is’,
    } );
}

Here’s the final result (run on the ibm.com website)

The rest library entry is

   w = cj.add(“builtwith”);
      w.add (“restType”, ERRESTTYPE.erSingleQuery);
      w.add (“url”, “http://api.builtwith.com/api.json?lookup=”);
      w.add (“results”, “Technologies”);
      w.add (“treeSearch”, false);
      w.add (“ignore”);
      w.add (“append”,”&key=”);

Next post, I’ll show you how to get the same api Key into Excel, so you won’t need to hard code it into your VBA module.

(function() {
var po = document.createElement(‘script’); po.type = ‘text/javascript’; po.async = true;
po.src = ‘https://apis.google.com/js/plusone.js’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(po, s);
})();

Author: brucemcp

Leave a Reply

Your email address will not be published. Required fields are marked *