Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Setting Up the Imgur API

Matteo Stara
PLUS
Matteo Stara
Courses Plus Student 1,564 Points

Anybody managed to run this app successfully and actually get the images?

{ "data": { "error": "client_id and response_type are required", "request": "/oauth2/authorize", "method": "GET" }, "success": false, "status": 400 }

This is what I get whem I run the app, even after adding the missing code (which is, by the way, kind of unacceptable, as we pay for these courses).

I have no errors in logcat to work with, anyone has any idea on how to proceed?

8 Answers

Hi Matteo. Just a quick update. I did manage to get the images from the API. I believe the API is indeed a bit capricious ^^ as I spent a few hours debugging yesterday, finally getting the images (from a 403, just like you, then an authorized app without images), and yet, when I re-did the project from scratch this morning (registering a new app on Imgur), it worked first time around! I would advise you to clear your cookies and caches from your browser and uninstall the app before trying again. Best of luck! I wish I had more tips to give you than wishing you good luck, but (apart from the cookies and caches), it seems to be down to that in the end. I'm happy to share my code with you if you want to.

To those interested, I also found a small error in the code. It doesn't have any impact on getting the images first time around but will do when the token expires. As the code is now, the expires_in response is not saved in the sOAuthCredentials SharedPreferences. The reason is that commit() is not called in OAuthUtil.set(). As a result, getLong(EXPIRES_IN) always returns -1 (the default value set in OAuthUtil.getLong()). So, as such, isAuthorized() will always be true (unless we decide to time travel).

The solution is to:

  • add commit() to OAuthUtil.set() for Long values:

    public static void set(String key, Long value) {
        editSharedPrefs().putLong(key, value).commit();
    }
    
  • Change the relational operator in OAthUtil.isAuthorized() to more than:

    public static boolean isAuthorized() {
        return get(ACCESS_TOKEN) != null &&          
                   getLong(EXPIRES_IN) > System.currentTimeMillis();
    }
    

Hope this helps :)

Matteo Stara
Matteo Stara
Courses Plus Student 1,564 Points

Hey Lauren,

Thank you so much for letting me know.

No matter what I do, how many times I tried, which login I use, how many times I uninstall the app, it always gets the same dreadful 403.

My code is up here, see if you can spot any mistake, or run it succesfully with your account credentials: https://github.com/Memnoc/Portfolio-Apps-RetrofitOAuth2

Thanks!

Hi Matteo. Sorry to hear that :( Sure, I'll have a look later and let you know.

Hi Matteo. I've just had a look and there are 2 little things you need to fix:

  1. In AndroidManifest.xml In the <data> tag of the redirecting URL <intent-filer>, you do not need to add "https://" to the host attribute as the scheme attribute set right above it does that for you. Just amend the host attribute and you will be redirected properly to the app:

    <data
    android:scheme="https"
    android:host="smartdroidworkshop"
    android:port="88"
    android:pathPattern="/.*"/>
    
  2. In MainActivity.java You just forgot to add the code to onResponse(). So even with the Manifest fixed, you would get redirected to the app but get a blank screen as the data is fetched but nothing is done with it. onResponse() should be:

    @Override
    public void onResponse(Call<Basic<ArrayList<Image>>> call, Response<Basic<ArrayList<Image>>> response) {
        if (response.code() == HttpURLConnection.HTTP_OK) {
            ((ImageAdapter) recyclerView.getAdapter()).swap(response.body().data);
        } else {
            Snackbar.make(upload, "Failed :(", Snackbar.LENGTH_SHORT).show();
        }
    

Then it works perfectly :) Let me know how it goes

Matteo Stara
PLUS
Matteo Stara
Courses Plus Student 1,564 Points

The error now is:

{ "data": { "error": "Unauthorized", "request": "/oauth2/authorize", "method": "POST" }, "success": false, "status": 403 }

Hi. I haven't done the course and don't have access to the video as I'm not a PRO student, but the error seems to be with your authentication. Maybe this page will help.

I'll probably do the course later on this week or next week.

Matteo Stara
PLUS
Matteo Stara
Courses Plus Student 1,564 Points

Hey Lauren,

Thanks for getting back to me.

This is not something that has anything to do (at least, so it seems) with the code itself, rather, with the Imgur API. Let me know if you manage to get your app authprized anyway.

Thanks!

Hi Matteo. I see. Ok, I'll let you know how it goes. Fingers crossed! :)

Matteo Stara
PLUS
Matteo Stara
Courses Plus Student 1,564 Points

Lauren -

Ok for the Manifest, that was a bubu and I had it right, not sure why I have uploaded the wrong one. Where does that the onResponse() method come from? The video 2 ends with onFailure() in MainActivity, there is no such method as the one you posted, and the app works without it?

Updae: now I can pass the authentication part, but the code provided is in the third video. onResponse() is only overridden at the end of the second one, and the app is working. So, again, half-baked tutorials from Treehouse, and a lot of wasted time.

I am still unable to retrieve any image.

And now, the 403 again, changed nothing LOL This must be the most frustrating experience/project I have ever worked on. I did well cancelling my subscription, this is not worth paying to be honest. Thanks Lauren for all the help though, much appreciated!

It's in the fetchAccountImages() method. The onResponse() and onFailure() methods are added automatically by Android Studio when you create a new Callback. But you need to write what to do when you get the data from the network (in onResponse()) or when that process fails (in onFailure()). The app would work without any code inside onResponse() but the images would not be displayed as we're not specifying what to do with the data we've just fetched from Imgur. Check the 2nd video @15:24

You're welcome Matteo. Just try clearing your cookies/caches from your browser and uninstall your app before running the app again. It should work. I find that I had to do that every time before trying again or it would fail. I agree that this project is pretty frustrating until you get it to work.

Matteo Stara
PLUS
Matteo Stara
Courses Plus Student 1,564 Points

Lauren - You're right, the code is at the time-stamp you have indicated. till, cleaned everything, multiple times, changed emulator, created new one, tried a new app, a new account, still nothing. Best I have obtained is passing the 403, but no images.

There are more fun ways to learn Retrofit, and honestly, this is not fun or entertaining, or instructional :)

I'm sorry to hear that Matteo. You're right, it's no fun like that. I'll just ask one last (and maybe stupid) question but did you add any images to your Imgur account? (From the web). Your code works fine my side (using my credentials).

Matteo Stara
PLUS
Matteo Stara
Courses Plus Student 1,564 Points

Hey Lauren,

Yeah I thought about that too, no stupid at all. I did, but now I cannot manage to run the project at all, back on the 403. If the code works okay your side, it's good news, but the unreliability of this API combined with the lack of maintenance of these tutorials (it's the third time I have to adjust 60% of the code to even get it to work, let alone run it), makes this whole thing extremely time-consuming, and it's a shame, because the idea it's actually valuable.

Just out the blue, I did get the images now. What more can we say?

Lol! I called it "magic" the other day. I understand the pain. I took the few hours I spent on the project the other day as debugging practice, although it's not the original purpose of that workshop :) I'm glad it's finally working.

Matteo Stara
PLUS
Matteo Stara
Courses Plus Student 1,564 Points

Magic sounds about right, also added the POST part now. I think I'll finish the workshop, and perhaps use the same technique to work with another API, maybe less cumbersome, Unsplash or Fickr sounds like good candidates. Feel free to follow my repo, it's a bit of a dumpster for training projects, but there are some cool stuff in there. Thanks again for helping out, much appreciated!

Sounds like a good plan. You're welcome :)

Matteo Stara
PLUS
Matteo Stara
Courses Plus Student 1,564 Points

Lauren - Did you manage to get the anonymous upload working? edit: I did it too, completed the workshop

Yes. But I had to clear the browser's cookies/cache (and maybe even uninstall the app/clear the app data as well, can't remember) to get it to work.