1

i cant seem to get google's new url shortener api to work with jquery's post method:

  $(document).ready(function() {
   $.post("https://www.googleapis.com/urlshortener/v1/url", { longUrl: "http://www.google.com/"},
      function(data){
        console.log("data" + data);
      });
   $('body').ajaxError(function(e, xhr, settings, exception) {
       $(this).text('fail'+e);
   console.log(exception);
   });
  });

all of this gives me an empty (data) response AND an empty (exception) response. any ideas?

ive also tried this with no success:

   $.ajax({
 type: 'POST',
 url: "https://www.googleapis.com/urlshortener/v1/url",
 data: { longUrl: "http://www.google.com/"},
 success: success,
 dataType: "jsonp"
   });
rahim
  • 11
  • 1
  • 2
  • 1
    I ran your top code block and firebug reports a 405 Method Not Allowed response from google. See http://stackoverflow.com/questions/4690927/problem-with-implementing-google-url-shortener-api for similar discussion – tawman Jan 14 '11 at 19:30

7 Answers7

2

I was having the same problem of not being able to use Google Shortener API with jQuery, but after some research I got around with a simple JSON.stringify on the data

My final code looks like this:

    window.googleShAPI = 'YOUR_KEY';
    window.getShortURL = function(url,cb){
        $.ajax({
             url:'https://www.googleapis.com/urlshortener/v1/url?key='+window.googleShAPI,
             type:"POST",
             data:JSON.stringify({longUrl:url}),
             contentType:"application/json",
             dataType:"json",
             success: function(data){ cb(data.id); }
        });
    }

    // example of use:
    window.getShortURL('http://www.google.com/',function(u){console.log('ShortURL is:'+u);})
Rafael Z. B. Bravo
  • 1,022
  • 10
  • 23
1

You need API key in order to use this API

This method requires one query parameter: Your API key (using the key query parameter). Note: You can omit the query parameter when making a limited number of calls, such as during the testing phase.

also response return in JSON

$.post("https://www.googleapis.com/urlshortener/v1/url?key=enter-your-api-key", { longUrl: "http://www.google.com/"},
  function(data){
    console.log("data" + data);
  }, "json");
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
aifarfa
  • 3,939
  • 2
  • 23
  • 35
0

Best Solution found on this :

<html>
<head>
<title>URL Shortener using Google API. http://goo.gl </title>
<script src="https://apis.google.com/js/client.js" type="text/javascript"> </script>
</head>
<script type="text/javascript">
function load() {
    gapi.client.setApiKey('[GOOGLE API KEY]');
    gapi.client.load('urlshortener', 'v1', function() { 
        document.getElementById("result").innerHTML = "";

        var Url = "http://onlineinvite.in";
        var request = gapi.client.urlshortener.url.insert({
            'resource': {
            'longUrl': Url
            }
        });
        request.execute(function(response) {

            if (response.id != null) {
                str = "<b>Long URL:</b>" + Url + "<br>";
                str += "<b>Test Short URL:</b> <a href='" + response.id + "'>" + response.id + "</a><br>";
                document.getElementById("result").innerHTML = str;
            }
            else {
                alert("Error: creating short url \n" + response.error);
            }
        });
    });
}
window.onload = load;
</script>
<body>
<div id="result"></div>
</body>
</html>

Please replace the [GOOGLE API KEY]

0

I assume you have read the Google URL Shortener API reference instructions, but I do not see an API key in your code snippet:

POST https://www.googleapis.com/urlshortener/v1/url?key={key}
tawman
  • 2,478
  • 1
  • 15
  • 24
  • yes i have tried sending the key in the URL as well as a post param with the same result – rahim Jan 14 '11 at 19:18
0

try setting the contentType, like this:

$.ajax({
 type: 'POST',
 contentType: "application/json"
 url: "https://www.googleapis.com/urlshortener/v1/url",
 data: { longUrl: "http://www.google.com/"},
 success: success,
 dataType: "jsonp"
   });
Arthur Neves
  • 11,840
  • 8
  • 60
  • 73
0

Try using getJson Method:

url="https://www.googleapis.com/urlshortener/v1/url";
data = {
    key:"your-api-key",
    shortUrl:"http://www.google.com"
};
$.getJSON(url, data, function(data, textStatus){
    if(data.status=="OK"){
        alert(data.longUrl);
        }
    });
Anyul Rivas
  • 655
  • 2
  • 12
  • 31
0

You can't access Google's URL Shortening API using any of the methods currently described in these answers, due to cross-domain script access restrictions. See the discussion here for solutions: Cross Domain Issue with implementing Google URL shortener API

Thanks to tawman for this answer, but it was hidden in the comments and I didn't see it at first.

Community
  • 1
  • 1
Eric Nguyen
  • 40,312
  • 4
  • 25
  • 31