0

I'm using loopj to create a GET request to fetch data from GitHub API v3. However the status code 403 keeps returning in the callback and i've tried every possible solution i could think of.

Here is the code im using, this method is called in a onclicklistener of a button. I created using sample codes from other apps that i developed and following instructions from loopj docs.

private void getDataFromGit() {


    final AsyncHttpClient client = new AsyncHttpClient();
    final String url = "https://api.github.com";
    client.addHeader("-H ", "application/vnd.github+json");


    client.get(getApplicationContext(), url,null, ContentType.APPLICATION_JSON.getMimeType(),  new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            Toast.makeText(getApplicationContext(), "On Success: ", Toast.LENGTH_SHORT).show();

        }
        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            Toast.makeText(getApplicationContext(), "On Success: ", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            Toast.makeText(getApplicationContext(), "On Failure: 1 " + String.valueOf(statusCode) + " "+  errorResponse.toString(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
            Toast.makeText(getApplicationContext(), "On Failure: 2 " + String.valueOf(statusCode) + " " + errorResponse.toString(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            Toast.makeText(getApplicationContext(), "On Failure: 3 " + String.valueOf(statusCode) + " " + throwable.toString(), Toast.LENGTH_SHORT).show();
        }
    });
}

Acording to git hub api docs this request needs no autentication.

https://developer.github.com/v3/media/#request-specific-version

The toast are returning a failure callback with a status code of 403. I tried several endpoints, tried to add the sugested header from the API but it all returns the same status code.

2018-10-22 21:34:47.325 4685-4685/devarthur.post.gitrepos E/GET: On error cz.msebera.android.httpclient.client.HttpResponseException: Forbidden

What else could i do to get a status code 200 using loopj?

EDIT.

So i got some advice and found more information about 403 error with this API. Looks like there has to be a User-Agent parameter by default. Im trying to create the request using this as an example, if it works i will post this as an Answer.

Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.

1 Answers1

0

The http header should look like: “Accept: application/vnd.github+json“ “-H” is only needed when using curl. You can try this code: client.addHeader(“Accept”, “application/vnd.github+json”)

J.Gerbershagen
  • 316
  • 1
  • 3
  • Thanks for taking your time to answer my question. I had forgot about it since i was able to correct myself. Your are almost correct in your answer, but i found out by reading the documentation with more caution that i nedded an access token for this get method. I need to get home to post the code as an aswner, but thanks for your time. – Arthur Gomes da Silva Nov 08 '18 at 20:01