Home > Mobile >  How to fix api call working in browser but 403 in android studio
How to fix api call working in browser but 403 in android studio

Time:09-20

Hi i'm wondering why my API call works in browser but when called in Android studio I get a 403 error. The Debug

D/OkHttp: <-- 403 https://api.johnlewis.com/search/api/rest/v2/catalog/products/search/keyword?q=dishwasher&key=AIzaSyDD_6O5gUgC4tRW5f9kxC0_76XRC8W7_mI (106ms)
D/OkHttp: server: AkamaiGHost
D/OkHttp: mime-version: 1.0
D/OkHttp: content-type: text/html
D/OkHttp: content-length: 350
D/OkHttp: expires: Tue, 20 Sep 2022 10:27:24 GMT
D/OkHttp: date: Tue, 20 Sep 2022 10:27:24 GMT
D/OkHttp: strict-transport-security: max-age=31536000 ; includeSubDomains
D/OkHttp: <HTML><HEAD>
D/OkHttp: <TITLE>Access Denied</TITLE>
D/OkHttp: </HEAD><BODY>
D/OkHttp: <H1>Access Denied</H1>
D/OkHttp: You don't have permission to access "http&#58;&#47;&#47;api&#46;johnlewis&#46;com&#47;search&#47;api&#47;rest&#47;v2&#47;catalog&#47;products&#47;search&#47;keyword&#63;" on this server.<P>

The URL: https://api.johnlewis.com/search/api/rest/v2/catalog/products/search/keyword?q=dishwasher&key=AIzaSyDD_6O5gUgC4tRW5f9kxC0_76XRC8W7_mI

Code Snippet of api call:

private fun getUserList() {
        var retrofit = RetrofitClient.getInstance()
        var apiInterface = retrofit.create(SimpleApi::class.java)
        lifecycleScope.launchWhenCreated {
            try {
                val response = apiInterface.getPost()
                if (response.isSuccessful) {
                    var json = Gson().toJson(response.body())
                    if (response.body()?.data?.size!! <= 0) {
                        Toast.makeText(
                            this@MainActivity,
                            "No Data ",
                            Toast.LENGTH_LONG
                        ).show()
                    } else {
                        txtData.text = json
                    }

                } else {
                    Toast.makeText(
                        this@MainActivity,
                        response.errorBody().toString(),
                        Toast.LENGTH_LONG
                    ).show()
                }
            }catch (Ex: Exception){
                Log.e("Error",Ex.localizedMessage)
            }
        }

    }

Any Ideas why android studio might be giving me back a 403 error? Many Thanks

CodePudding user response:

The HTTP 403 Forbidden client error status response code indicates that the server understood the request but refuses to authorize it.

This status is similar to 401, but in this case, re-authenticating will make no difference. The access is permanently forbidden and tied to the application logic (like an incorrect password).

CodePudding user response:

i just implement it in some code you can check it below :

 request = new Request.Builder()
                    .url("https://api.johnlewis.com/search/api/rest/v2/catalog/products/search/keyword?q=dishwasher&key=AIzaSyDD_6O5gUgC4tRW5f9kxC0_76XRC8W7_mI")
                    .get()
                    .addHeader("Cache-Control","no-cache")
                    .addHeader("User-Agent",System.getProperty("http.agent"))
                    .build();

by using above code i am able to get 200 response in mobile app. i used OkttpClient for this just add header in retrofit to solve 403 issue.

Response{protocol=h2, code=200, message=, url=https://api.johnlewis.com/search/api/rest/v2/catalog/products/search/keyword?q=dishwasher&key=AIzaSyDD_6O5gUgC4tRW5f9kxC0_76XRC8W7_mI}

Enjoy !!! Happy Coding !!

  • Related