1

I'm trying to call the Google URL Shortener API in my python code:

def shorternUrl():
        API_KEY = "AIzaSyCvhcU63u5OTnUsdYaCFtDkcutNm6lIEpw"
        apiUrl = 'https://www.googleapis.com/urlshortener/v1/url'
        longUrl = "http://www.cnn.com"
        headers = {"Content-type": "application/json"}
        data = {"longUrl": longUrl}
        h = httplib2.Http('.cache')
        try:
            headers, response = h.request(apiUrl, "POST", urllib.urlencode(data), headers)
            print response

        except Exception, e:
            print "unexpected error %s" % e

But I keep getting this error:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "parseError",
    "message": "Parse Error"
   }
  ],
  "code": 400,
  "message": "Parse Error"
 }
}

I'm not using the Google API for Python. Where i'm I going wrong?

1 Answer 1

5

You need to send JSON in a POST, not URL encoded data:

import json

# Rest of your code

headers, response = h.request(apiUrl, "POST", json.dumps(data), headers)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.