Home > Net >  Yelp API Validation Errors(400) - Android Studio
Yelp API Validation Errors(400) - Android Studio

Time:06-22

I want to do a simple experiment with the yelp api, but I'm stuck at startup and keep getting validation error. I checked and refreshed the API_key and BaseURL etc but the error persisted.

MainActivity.kt

private const val TAG = "MainActivity"
private const val BASE_URL = "https://api.yelp.com/v3/"
private const val API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val retrofit = Retrofit.Builder().baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        val yelpService = retrofit.create(YelpService::class.java)
        yelpService.searchRestaurants("Bearer $API_KEY", "Avocado Toast","New York").enqueue(object : Callback<Any> { 
                override fun onResponse(call: Call<Any>, response: Response<Any>) {
                Log.i(TAG, "onResponse $response")
            }
            override fun onFailure(call: Call<Any>, t: Throwable) {
                Log.i(TAG, "onFailure $t")
            }

        })
    }
}

YelpService.kt

public interface YelpService {
    @GET("businesses/search")
    fun searchRestaurants(
        @Header("Authorization") authHeader: String,
        @Query("term") searchTerm: String,
        @Query("location") location: String) : Call<Any>
}

I will be grateful if you could help me.

enter image description here

CodePudding user response:

{"error": {"code": "VALIDATION_ERROR", "description": "Authorization is a required parameter.", "field": "Authorization", "instance": null}}

enter image description here

CodePudding user response:

I tested service by myself now. For URL:

https://api.yelp.com/v3/businesses/search?term=delis&latitude=37.786882&longitude=-122.399972

You have to add term, latitude, and longitude as a RequestParam.

For header you have to add just: Authorization and for value Bearer API_KEY

As i understand that problem is you don't sent the location as a RequestParam. I mean you have to send with url format as i shared.

After that all the response:

{
    "businesses": [
        {
            "id": "FmGF1B-Rpsjq1f5b56qMwg",
            "alias": "molinari-delicatessen-san-francisco",
            "name": "Molinari Delicatessen",
            "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/4gRY9rVs8JyHvngljSzXyA/o.jpg",
            "is_closed": false,
            "url": "https://www.yelp.com/biz/molinari-delicatessen-san-francisco?adjust_creative=yIYqZpTZ3iCNqMi0kAtGAQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=yIYqZpTZ3iCNqMi0kAtGAQ",
            "review_count": 1233,
            "categories": [
                {
                    "alias": "delis",
                    "title": "Delis"
                }
            ],
            "rating": 4.5,
            "coordinates": {
                "latitude": 37.79838,
                "longitude": -122.40782
            },
            "transactions": [
                "pickup",
                "delivery"
            ],
            "price": "$$",
            "location": {
                "address1": "373 Columbus Ave",
                "address2": "",
                "address3": "",
                "city": "San Francisco",
                "zip_code": "94133",
                "country": "US",
                "state": "CA",
                "display_address": [
                    "373 Columbus Ave",
                    "San Francisco, CA 94133"
                ]
            },
            "phone": " 14154212337",
            "display_phone": "(415) 421-2337",
            "distance": 1465.2460213942109
        }
    ],
    "total": 782,
    "region": {
        "center": {
            "longitude": -122.399972,
            "latitude": 37.786882
        }
    }
}

NOTE: I added limit end of URL, it's optional.

  • Related