Using the UrlShortener API in a custom Spreadsheet function
Asked Answered
M

1

1

I would like to use an API (url shortener) with a public google Add-on. For the moment my code returns:

Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.

  • Is this possible?
  • If yes, Do I need an authentification Token?
    • If yes, what key type should I choose?
    • How can I implement the authorisation for this kind of use?
    • Do I need to pay for it?
  • If no, How could other add-ons use external APIs

Thanks a lot for your answers,

Mowry answered 24/5, 2015 at 23:30 Comment(6)
Are you familiar with the Google Add-On group? Google CommunityClipper
Well I have been told to ask questions on stack overflow ??so What else can I do with this group?Mowry
The Google documentation does direct people to stackoverflow, which gives people the impression that stackoverflow is the place for any general question. And then when people ask a general question, they get down votes. Your question is about Add-Ons, and that group is specifically for Add-Ons.Clipper
Ok well thank you Sandy I'll find my answer on this group, but even if it's a pretty general question, I felt like it could help people understanding the basic of API inplementation on various plans. Thanks anyway :)Mowry
You may still get an answer here. There is the "official" way that stackoverflow is supposed to work, and then there's reality.Clipper
The daily free quota is 1,000,000 requests. How are your set up? Are you using the Url Shortener Advanced service with the api enabled in the dev. console?Caribou
C
1

EDIT: The OP pointed out in the comments that this is a custom function. Custom function run with limited authorization. A complete list of what is available is at:
https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services

Below uses the REST API to get the shortened url. This will work with custom functions. You will just need to enable the URL Shortener API and generate a Server API Key. Use the IPs at the following link for your server api key:
https://developers.google.com/apps-script/guides/jdbc#setup_for_google_cloud_sql

/**
 * Returns a shortened URL of the input.
 *
 * @param {string} longUrl The long URL to shorten.
 * @return The shortened url.
 * @customfunction
 */
function getShortUrl(longUrl) {

   var payLoad = {"longUrl": longUrl};
   var apiKey = PropertiesService.getScriptProperties().getProperty("ServerApiKey");
   var url  = "https://www.googleapis.com/urlshortener/v1/url?key="+ apiKey;
  var options = { method:"POST",
                 contentType:"application/json",
                 payload:JSON.stringify(payLoad),
                 muteHttpExceptions:true};

  var response = UrlFetchApp.fetch(url, options);
  if(response.getResponseCode() != 200){throw new Error("Unable to shorten url");}
  return JSON.parse(response).id;
}

Original Post


Here is a quick primer on using the UrlShortener advanced service. You will need to turn on the service and activate the Url Shortener api in the developers console. This will give you a quota of 1,000,000 requests per day for your add-on.

function myFunction() {
  var url = UrlShortener.newUrl();
  url.longUrl = "http://www.example.org";  
  var short = UrlShortener.Url.insert(url);
  Logger.log(short);

  //list all users shortened urls
  Logger.log(UrlShortener.Url.list());
}
Caribou answered 25/5, 2015 at 20:22 Comment(4)
well everything works fine when I use it inside the IDE but I cannot use it as custom function. Is it related to the authorization of my app? Here is the message I get "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup." thanks in advanceMowry
Ah custom functions are a different beast. Ill edit the answer.Caribou
Thanks, because I've dealt already with trigger issues but still don't completely get itMowry
Ok many thanks, it works now. But i still didn't make it go live.Mowry

© 2022 - 2024 — McMap. All rights reserved.