Python Foursquare API request error when trying to get venue details

148 Views Asked by At

I try to make a request from the foursquare api. If I change the request URL it does no longer get me a result

when I make a request with this line of code everything is fine:

def venue_details():
    detail_results = requests.get('https://api.foursquare.com/v2/venues/4e96cf73b8f7d8c690f48384/?client_id=[my client id]&client_secret=[my client secret]&v=20180604').json()

When I make a request with this line of code I get an error:

`VENUE_ID = '4e96cf73b8f7d8c690f48384'
CLIENT_ID = '[my client id]'
CLIENT_SECRET = '[my client secret]'
VERSION = '20180604'


url = 'https://api.foursquare.com/v2/venues/?venue_id={}/?client_id={}&client_secret={}&v={}'.format(
VENUE_ID,
CLIENT_ID, 
CLIENT_SECRET, 
VERSION
)

detail_results = requests.get(url).json()
{'meta': {'code': 400,
  'errorType': 'invalid_auth',
  'errorDetail': 'Missing access credentials. See https://developer.foursquare.com/docs/api/configuration/authentication for details.',
  'requestId': '63623c5231edc703599dc1da'},
 'response': {}}

I think I get the authentication error because it stops reading the url at "?venue_id"

2

There are 2 best solutions below

0
Todd S. On BEST ANSWER

I can see that you are using the legacy version of our Places API which now has limited support. Please switch to use v3 --> https://api.foursquare.com/v3/places/search

Our documentation should get you started --> https://location.foursquare.com/developer/reference/place-search

Please join our Discord if you need more direct help --> https://discord.gg/foursquaredevs

1
Olasimbo On

You can try it this way:

    def foresquare_location(lat, lon, radius):

        CLIENT_ID = 'XXXXXXXX'  # your Foursquare ID
        CLIENT_SECRET = 'XXXXXXX'  # your Foursquare Secret
        VERSION = '20180604'
        LIMIT = 100
        neighborhood_latitude = lat
        neighborhood_longitude = lon
        # limit of number of venues returned by Foursquare API
        url = 'https://api.foursquare.com/v2/venues/explore'
        params = dict(client_id=CLIENT_ID,
                      client_secret=CLIENT_SECRET,
                      v=VERSION,
                      ll=['{0},{1}'.format(neighborhood_latitude, neighborhood_longitude)],
                      categoryId=['XXXXXXXX'],
                      limit=LIMIT,
                      radius=radius,
                      sortByPopularity=1)

        resp = requests.get(url=url, params=params)
        data = json.loads(resp.text)
        venues = data['response']['groups'][0]['items'] 
        return data, venues