3

I'm trying to connect my code with google maps api(geocoding API) key. But when I run my code, the ValueError occurs. I don't know why. It might be silly, but please help me.

Here's my python code.

import httplib2 
import json 

def getGeocodeLocation(inputString): 
    google_api_key = "MY_API_KEY" 
    locationString = inputString.replace(" ", "+") 
    url = ('https://maps.googleapis.com/maps/api/geocode/json?address=%s & key=%s' % (locationString, google_api_key)) 
    h = httplib2.Http() 
    response, content = h.request(url, 'GET') 
    result = json.loads(content) 
    print "response header: %s \n \n" % response 
    return result

And here's the error.

>>> from geocode import getGeocodeLocation
>>> getGeocodeLocation("Dalls, Texas")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "geocode.py", line 10, in getGeocodeLocation
    result = json.loads(content)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Thanks.

+

When I edited the code from,

    response, content = h.request(url, 'GET') 
    result = json.loads(content) 
    print "response header: %s \n \n" % response 

to

    response, content = h.request(url, 'GET')  
    print "response header: %s \n \n" % response 
    result = json.loads(content)

The result was like this.

{u'status': u'OK', 
u'results': 
    [
    {u'geometry': 
        {u'location_type': u'APPROXIMATE', 
        u'bounds': 
            {u'northeast': {u'lat': 33.0237921, u'lng': -96.4637379}, 
            u'southwest': {u'lat': 32.617537, u'lng': -96.999347}}, 
        u'viewport': 
            {u'northeast': {u'lat': 33.0237921, u'lng': -96.4637379}, 
            u'southwest': {u'lat': 32.617537, u'lng': -96.999347}}, 
        u'location': {u'lat': 32.7766642, u'lng': -96.79698789999999}
        }, 
    u'address_components': 
        [
        {u'long_name': u'Dallas', 
        u'types': [u'locality', u'political'], 
        u'short_name': u'Dallas'}, 
        {u'long_name': u'Dallas County', 
        u'types': [u'administrative_area_level_2', u'political'], 
        u'short_name': u'Dallas County'}, 
        {u'long_name': u'Texas', 
        u'types': [u'administrative_area_level_1', u'political'], 
        u'short_name': u'TX'}, 
        {u'long_name': u'United States', 
        u'types': [u'country', u'political'], 
        u'short_name': u'US'}
        ], 
    u'place_id': u'ChIJS5dFe_cZTIYRj2dH9qSb7Lk', 
    u'formatted_address': u'Dallas, TX, USA', 
    u'types': [u'locality', u'political']
    }
    ]
}
Jinne
  • 41
  • 1
  • 7
  • 1
    The error occurs when json.loads() runs, so try printing the `response` and `content` variables before this call to see what they contain. Then you might see why the json call is failing. (It appears that `content` is not a valid JSON object). – Lindsay Ward Apr 04 '18 at 06:01
  • Thanks for helping! I tried, and the result was like this. [response header: {'date': 'Wed, 04 Apr 2018 06:22:50 GMT', 'status': '400', 'content-length': '1555', 'referrer-policy': 'no-referrer', 'content-type': 'text/html; charset=UTF-8'}] – Jinne Apr 04 '18 at 06:32
  • @JinneKim. Can you post `content`? – Rakesh Apr 04 '18 at 06:55
  • @Rakesh Sure. Please check the answer below. – Jinne Apr 04 '18 at 07:27

1 Answers1

1

You are having an error in your request. The status code is 400 Bad Request, and the content of your response is:

Your client has issued a malformed or illegal request. That's all we know.

There error seems to be on this line:

url = ('https://maps.googleapis.com/maps/api/geocode/json?address=%s & key=%s' % (locationString, google_api_key))

You shouldn't have blank spaces around the & sign:

url = ('https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s' % (locationString, google_api_key))
Gabor
  • 685
  • 2
  • 11
  • 25