2

again!
I created an embedded in spreadsheet script that use UrlShortener.Url.insert feature. My client wants to be able to make a new instances of this spreadsheet to share it with colleagues. I've implemented this feature, but when I begin to test new instance it's turned out that I have to enable URL Shortener API in my Google Developers Console.
I wonder if I can bypass this manual work using my script or only I can do is to provide client with instructions how to do it?

Update: Sandy Good recomends to use UrlFetch.fetch() to get the short link, but this code:

function test_short_link() {
  var options =
     {
       'longUrl': 'http://www.google.com/',
       'muteHttpExceptions': true
     };

   var result = UrlFetchApp.fetch("https://www.googleapis.com/urlshortener/v1/url",
       options);
   Logger.log(result);
}

returns this:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Required parameter: shortUrl",
    "locationType": "parameter",
    "location": "shortUrl"
   }
  ],
  "code": 400,
  "message": "Required parameter: shortUrl"
 }
}

Looks like this topic

And this code

function test_short_link() {
  var options =
     {
       'longUrl': 'http://www.google.com/',
       'muteHttpExceptions': true,
       'method':'post'

     };
   var result = UrlFetchApp.fetch("https://www.googleapis.com/urlshortener/v1/url",
       options);
   Logger.log(result);
}

bring us:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "userRateLimitExceededUnreg",
    "message": "User Rate Limit Exceeded. Please sign up",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "User Rate Limit Exceeded. Please sign up"
 }
}
Community
  • 1
  • 1
Karen Fisher
  • 747
  • 1
  • 8
  • 25
  • I can't find anything that would allow accessing somebody else's Developers Console. I'd think that would be a security issue. So, even though I can't find anything saying that it's not possible, I'd be **SHOCKED** if it was possible. You can use the URL Shortener API with `UrlFetch.fetch()`. That doesn't require the Developers Console. – Alan Wells Mar 04 '15 at 13:03
  • Thank you for answer. But looks like UrlFetch doesn't work either. I updated the quesition. – Karen Fisher Mar 04 '15 at 13:52

2 Answers2

4

Edit: No there is no way to access the Google Dev console through any method beside the website itself.

Here is the method to do this by UrlFetchApp. You need to pass the options in payload parameter, not in the UrlFetchApp options object. You also need to pass the current users OAuth token in the header. Of course you will need to modify this code as it hard codes the longUrl and does no error checking.

function ShortenUrl(){
var url = "https://www.googleapis.com/urlshortener/v1/url"

var payload = {"longUrl":"www.google.com"};

var parameters = { method : 'post',
                    headers : {'Authorization': 'Bearer '+ScriptApp.getOAuthToken()},
                    payload:JSON.stringify(payload),
                    contentType:'application/json',                    
                    muteHttpExceptions:true};

  var response = UrlFetchApp.fetch(url, parameters);
  Logger.log(response);

}
Spencer Easton
  • 5,642
  • 1
  • 16
  • 25
0

This can be done perfectly. Try this code:

function ShortenUrl(){
var url = 'https://www.googleapis.com/urlshortener/v1/url';
var apiKey = 'AIzBlNS-3HZdxKgwj-x30';
url += '?key=' + apiKey;
var payload = {"longUrl":"www.google.com"};

var parameters = { method : 'post',
                payload:JSON.stringify(payload),
                contentType:'application/json',                    
                muteHttpExceptions:true};

var response = UrlFetchApp.fetch(url, parameters);
Logger.log(response);
}
Pang
  • 9,564
  • 146
  • 81
  • 122