-1

I am trying to pull nearby venues from foursquare API using react native.

For beginning i tried to pull venues from some specified coordinates and here is my very simple code;

import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import axios from 'axios'

export default function App() {

  var venues;

  return (
    <View style={styles.container}>
      <Text>{this.venues} qwewqe</Text>
      <Button
  onPress={() => {
    this.getVenues()
  }}
  title="GET VENUES"
/>
    </View>
  );

}

getVenues = () => {

  const endpoint = 'https://api.foursquare.com/v2/venues/explore'
  const parameters = {

    CLIENT_ID:"*******",
    CLIENT_SECRET:"*******",
    section: "food",
    ll: "   40.74224,-73.99386",
    v: "20190909"

  }

  axios.get(endpoint + new URLSearchParams(parameters))
  .then(response => {
    console.log(response)
   this.venues = response;
    return response;
  })
  .catch(error => {
    console.log("ERROR >>    " + error)
  })


}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Console output:

ERROR >> Request failed with error code 400

Well, 400 is as you know is bad request. so code looks working for some point.

I did not understand what is wrong with my request ? also checked the docs but didnt. For error details i ran on browser but i am getting getVenues is not a function error.

Thanks for your support...

Hbarna
  • 425
  • 3
  • 16
ugrdursun
  • 351
  • 1
  • 4
  • 19

1 Answers1

1

You are creating wrong URL with provided syntax because

axios.get(endpoint + new URLSearchParams(parameters))

generates something like

https://api.foursquare.com/v2/venues/exploreCLIENT_ID=*******&CLIENT_SECRET=*******&section=food&ll=+++40.74224%2C-73.99386&v=20190909

so maybe try something like axios.get(endpoint + "?" + new URLSearchParams(parameters))

Also I've just seen that they return 400 for unauthorized request with error type

"errorType":"invalid_auth"

so be sure to check what exactly is your error type.

zhuber
  • 5,364
  • 3
  • 30
  • 63
  • Thanks for your help, with the question mark or without it both getting the same error >>   "errorType":"invalid_auth","errorDetail":"Missing access credentials. What kind of credentials is this asking i couldnt understand ? – ugrdursun Sep 19 '19 at 17:59
  • 1
    Now i changed capital CLIENT ID's to lowercases and worked with question mark. Thank you for your support, appriciated ! – ugrdursun Sep 19 '19 at 18:13