0
votes

I am attempting to get a user's Reddit front page. I have successfully received an Auth Token via the Token Retrieval (code flow). I have managed to get the expected JSON response via Postman, but cannot produce the same results with Retrofit. The request seems to be timing out as onFailure() is being triggered in the callback. I am using the scopes: identity, mysubreddits, and read.

Additional note: I have got a 401 and 403 response with the code below when using insufficient scopes and using an expired Auth Token respectively.

Relevant constants:

redditToken = (actual auth token String)
RedditConstants.REDDIT_BASE_URL_OAUTH2 = "https://oauth.reddit.com"

Relevant method Section:

if (redditToken != null) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RedditConstants.REDDIT_BASE_URL_OAUTH2)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Api api = retrofit.create(Api.class);

        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "bearer " + redditToken);
        headers.put("User-Agent", RedditConstants.REDDIT_USER_AGENT);

        Call<RedditFeed> call = api.getFeed(headers);
        call.enqueue(new Callback<RedditFeed>() {
            @Override
            public void onResponse(Call<RedditFeed> call, Response<RedditFeed> response) {
                Log.d("FINDME", "response "+ response.toString());

                if (response.isSuccessful()) {
                    Log.d("FINDME", "response was a success! we got the feed!");
                } else {
                    Log.d("FINDME", "responce was not successfull triggered");
                }
            }
            @Override
            public void onFailure(Call<RedditFeed> call, Throwable t) {
                Log.d("FINDME", "onFailure called from populateRedditFeed");
            }
        });
    } else {
        Toast.makeText(this, "Please Login with Reddit", Toast.LENGTH_SHORT).show();
    }

Retrofit Interface:

public interface Api {
    @GET(".")
    Call<RedditFeed> getFeed (
            @HeaderMap Map<String, String> headers
    );
}

Log Results:

D/NetworkSecurityConfig: No Network Security Config specified, using 
platform default
I/zygote: Do full code cache collection, code=123KB, data=105KB
After code cache collection, code=111KB, data=79KB
D/FINDME: onFailure called from populateRedditFeed

Postman Success:

enter image description here

1
Print the throwable in the onFailure, it will give an idea what the error is. Post it here so we can help - Kushan
Without any modifications to the code. I am now getting a successful response. Before I was at Starbucks and now i'm using my home WiFi, perhaps that has something to do with it. I'll test it again tomorrow at Starbucks to confirm. - Zachery Osborn
Haha cool :) good to know.... I could see that the code was fine and since you pointed out the 401 and 403 codes, i knew you knew about the auth token... That's why i had asked about the throwable log, anyway keep the log, it'll help you. - Kushan

1 Answers

0
votes

After many starts and stops, seemingly randomly getting either a 200 or calling onFailure() I discovered the problem in one of my Retrofit model classes. The JSON response from Reddit contains a field that can either be a long or boolean. I had it defined as a boolean in my java class which threw an llegalStateException when it was returned as a long.

type      name      description
special   edited    false if not edited, edit date in UTC epoch-seconds 
                    otherwise. NOTE: for some old edited comments on reddit.com, this will 
                    be set to true instead of edit date.

*I'm unsure how to deal with this duality of types in java so for now I've commented out the field and the code works as expected.