Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I goo.gl-shorten a url in js?

Prereq: create yourself an API key for urlshortener at https://code.google.com/apis/console/

There are lots of docs for various ways of turning a goo.gl url into the original URL via the js get api, e g: here, here and here -- and at least the first one even works.

If I tweak that one ever so slightly to use the insert api to convert a url to a tiny url, passing a { "longUrl": "https://codepen.io/" } instead, though, it breaks. Try it at http://codepen.io/johan/full/EHbGy#YOUR-API-KEY-HERE if you like, or run this somewhere:

<script>
var api_key = 'YOUR-API-KEY-HERE';

function makeRequest() {
  var request = gapi.client.urlshortener.url.insert({
    'longUrl': 'https://codepen.io/'
  });
  request.execute(function(response) {
    alert(JSON.stringify(window.got = response));
  });
}

function load() {
  gapi.client.setApiKey(api_key);
  gapi.client.load('urlshortener', 'v1', makeRequest);
}
</script>
<script src="https://apis.google.com/js/client.js?onload=load"></script>

...it just responds with an error:

{ "code": 400
, "message": "Required"
, "data": 
  [ { "domain": "global"
    , "reason": "required"
    , "message": "Required"
    , "locationType": "parameter"
    , "location": "resource.longUrl"
    }
  ]
, "error": 
  { "code": 400
  , "message": "Required"
  , "data": 
    [ { "domain": "global"
      , "reason": "required"
      , "message": "Required"
      , "locationType": "parameter"
      , "location": "resource.longUrl"
      }
    ]
  }
}

Suggestions? (No, it doesn't work any better if you change the url.insert parameter to an object with a resource.longUrl key – or just passing the url without a wrapper object.)

like image 944
ecmanaut Avatar asked Oct 31 '12 21:10

ecmanaut


People also ask

How do you short a URL in Javascript?

To create a URL shortener, we'll need a text input area and a button, then add the script tag. Create the index. php file inside the shorten-url directory and open it using VS code. In folder shorten-url , we will also add script.

How do I permanently shorten a URL?

You can shorten a URL by using an URL shortener website, which will shrink your URL for free. Popular link shorteners on the internet include Bitly, TinyURL, and Rebrandly. You'll need a premium account on these sites to unlock the full range of link customization options.

How do I shorten a Google script URL?

You can use Google Apps Script with the Google URL Shortener API to convert any long URL into a short one served through the goo.gl domain. Make sure you replace key with your own key from the Google Console dashboard. You can also bit.ly to shorten URLs.


2 Answers

It's not super clear in the docs or error message, but your request should look like the following and all will be well:

var request = gapi.client.urlshortener.url.insert({
    'resource': {'longUrl': 'https://codepen.io/'}
});
like image 96
doublesharp Avatar answered Oct 24 '22 04:10

doublesharp


I think I'll drop the messy client library for this, when it turns out I can do it five lines of coffescript instead of loading all that cruft, as I already have jQuery around anyway: http://codepen.io/johan/pen/puJyH

api = 'https://www.googleapis.com/urlshortener/v1/url'
api += "?key=#{key}"  if key = location.search.slice 1

$.ajax
  url: api
  type: 'POST'
  data: JSON.stringify(longUrl: url)
  contentType: 'application/json'
  success: (got) ->
    alert "shortened url: #{got.id}"
like image 27
ecmanaut Avatar answered Oct 24 '22 05:10

ecmanaut