'Google Civic Information API throwing error in Python
I'm trying to utilize pygci to pull voting district data for a particular address. I've enabled the Google Civic Information API for the listed key, but all I'm getting is
"Response was not valid Json"
when I try to run this code:
*from pygci import GCivicInfo, GCivicInfoError
API_KEY = 'AIzaSyA1EBhUzaHBaaFB8H7LiWfVQsf0KnuwRSs'
address = "4935 Spruce St. Philadelphia, PA 19139"
CivicInfo = GCivicInfo(api_key=API_KEY)
try:
CivicInfo.get_representative_by_address(params=address)
except GCivicInfoError as e:
print(e)*
Solution 1:[1]
Looking at the docs for the underlying API, it ultimately needs the address to have the keyword "address" encoded as a URL parameter:
GET https://civicinfo.googleapis.com/civicinfo/v2/representatives?address=4935%20Spruce%20St.%20Philadelphia%2C%20PA%2019139&key=your_api_key
With the requests library, you encode parameters into the URL this way by passing it a dict. So I was going to suggest you try:
CivicInfo.get_representative_by_address(params={"address": address})
But looking at pygci, it appears get_representative_by_address overwrites params with ''. If you still get an error, there's may be a bug in the library, or I've misunderstood its code.
https://github.com/moosh3/pygci/blob/f80c336a4417f57ce80495e02f0a4a45eb0f4ce1/pygci/endpoints.py#L33
def get_representative_by_address(self, params):
"""Looks up political geography and representative
information for a single address
:param address: (optional) type=string
:param includeOffices: (optional) type=boolean, default=True
"""
address = ''
return self.get('/representatives?', params=address)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jack Deeth |
