Jump to content

Google Url Shorterner


gEEEk
 Share

Recommended Posts

Hello dudes.

I'm just messing around with the Google URL Shortner API just for knowledge purposes, but I am having some issues with variables in curl.

Its super simple, but its just for fun.

BTW, the script wont run if you try because there is no API key in that syntax.

Allright, I think the problem is with the variable handling in the curl syntax. But I'm not sure, I cant really see why it just wont use the user input from the previous lines...

echo "Gimme the URL boy!"
read url

curl https://www.googleapis.com/urlshortener/v1/url?key=XXXXXXXXXX \
  -H 'Content-Type: application/json' \
  -d '{"longUrl": "$url"}'

Its giving me this error:

(
    "{",
    " \"error\": {",
    "  \"errors\": [",
    "   {",
    "    \"domain\": \"global\",",
    "    \"reason\": \"invalid\",",
    "    \"message\": \"Invalid Value\",",
    "    \"locationType\": \"parameter\",",
    "    \"location\": \"resource.longUrl\"",
    "   }",
    "  ],",
    "  \"code\": 400,",
    "  \"message\": \"Invalid Value\"",
    " }",
    "}"

Link to comment
Share on other sites

I think that is the problem, however If I'm just issuing a simple: " echo $url " I'm getting the url from the user input. I think that curl has some issues handling

variables from a bash script.

Not sure though..

Link to comment
Share on other sites

echo "Gimme the URL boy!"
read url

curl https://www.googleapis.com/urlshortener/v1/url?key=XXXXXXXXXX \
  -H 'Content-Type: application/json' \
  -d '{"longUrl": "$url"}'

I think the problem is that you have the $url within single quotes. Single quotes will not do variable replacements, it will be passing the longUrl as $url rather than the value contained in $url. Possible Solution, swap your double and single quotes round on the -d line.

Link to comment
Share on other sites

Hmm..

{

"error": {

"errors": [

{

"domain": "global",

"reason": "internalError",

"message": "Internal Error"

}

],

"code": 500,

"message": "Internal Error"

}

}

The API dosent seem to like that I'm replacing the quotes. .

Whata jesus, those this mean that the API cant handle variables.. Atleast not if you're using it though curl?

Link to comment
Share on other sites

No idea what language that is, but can't you do something like:

echo "Gimme the URL boy!"
read url
json = '{"longURL": "' + $url + '"}'

curl https://www.googleapis.com/urlshortener/v1/url?key=XXXXXXXXXX \
  -H 'Content-Type: application/json' \
  -d $json

?

Link to comment
Share on other sites

The API dosent seem to like that I'm replacing the quotes. .

Whata jesus, those this mean that the API cant handle variables.. Atleast not if you're using it though curl?

It isn't the API, its the shell. Or rather your use of the quotes in the shell. Try something like

#!/bin/sh
echo "Gimme the URL boy!"
read url

curl https://www.googleapis.com/urlshortener/v1/url \
  -H 'Content-Type: application/json' \
  -d "{'longUrl': '$url'}"

and it should work fine, or it did when I tested it. The problem you had was that you were passing curl a JSON string to pass to the google's API. When you enclosed the JSON string in single quotes you were telling the shell to not expand any variables in it so

'{"longUrl": "$url"}'

will pass to the API

{"longUrl": "$url"}

When you enclosed the JSON string in double quotes you are telling the shell you want it to expand any variables it finds, so

"{'longUrl': '$url'}"

will pass to the api

{'longUrl': 'Whatever URL you put in the variable'}

Try playing with single and double quotes with variables in the shell using the echo command. After playing around a bit it should all start to make sense.

Link to comment
Share on other sites

Brilliant mate, solved the entire thing! I'm so happy I could probably kiss ya, no homo ofc. :)

Now, to finalize the script. I want it to GREP out the new URL. How would I go about that?

The output is:

{

"kind": "urlshortener#url",

"id": "http://goo.gl/XxXX",

"longUrl": "http://google.com/"

}

grep "id" dosent seem to work.. Do I need to set the specific character lenght to grep out? Or idk.. : /

Link to comment
Share on other sites

Now, to finalize the script. I want it to GREP out the new URL. How would I go about that?

I wouldn't use grep to do that I would use sed.

#!/bin/sh
echo "Gimme the URL boy!"
read url

curl https://www.googleapis.com/urlshortener/v1/url \
  -H 'Content-Type: application/json' \
  -d "{'longUrl': '$url'}" 2>/dev/null | sed -r -e '/"id"/ !d' -e '/"id"/ s|.*"id".*(http://.*)",|\1|'

First the "2>/dev/null" part throws away some of the output that curl is putting out via stderr on my machine.

Now the sed part has 3 parameters. The first ( -r ) sets sed to use more advanced regular expressions (On some OS's it is -E). The second parameter ( -e '/"id"/ !d' ) deletes any lines that don't have "id" in them. The third parameter ( -e '/"id"/ s|.*"id".*(http://.*)",|\1|' ) takes any line with "id" in it and pulls out the url. It then replaces the pattern with the url it has found.

This can be a little confusing to begin with so read up on sed to get to grips with it. The second parameter could be replaced by piping the stream though grep first and grepping for "id". There are also many other ways that you could achieve the same sort of results (e.g. using awk).

Link to comment
Share on other sites

Amazing dude, thanks for all your help. I ended up changing the -r parameter to -> -E since I am on OSX.

/ gEEEk

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...