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 trialMatteo Stara
Courses Plus Student 1,564 PointsAnybody 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
Lauren Moineau
9,483 PointsHi 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()
toOAuthUtil.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
Courses Plus Student 1,564 PointsThe error now is:
{ "data": { "error": "Unauthorized", "request": "/oauth2/authorize", "method": "POST" }, "success": false, "status": 403 }
Lauren Moineau
9,483 PointsHi. 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
Courses Plus Student 1,564 PointsHey 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!
Lauren Moineau
9,483 PointsHi Matteo. I see. Ok, I'll let you know how it goes. Fingers crossed! :)
Matteo Stara
Courses Plus Student 1,564 PointsLauren -
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!
Lauren Moineau
9,483 PointsIt'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
Lauren Moineau
9,483 PointsYou'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
Courses Plus Student 1,564 PointsLauren - 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 :)
Lauren Moineau
9,483 PointsI'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
Courses Plus Student 1,564 PointsHey 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?
Lauren Moineau
9,483 PointsLol! 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
Courses Plus Student 1,564 PointsMagic 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!
Lauren Moineau
9,483 PointsSounds like a good plan. You're welcome :)
Matteo Stara
Courses Plus Student 1,564 PointsLauren - Did you manage to get the anonymous upload working? edit: I did it too, completed the workshop
Lauren Moineau
9,483 PointsYes. 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.
Matteo Stara
Courses Plus Student 1,564 PointsMatteo Stara
Courses Plus Student 1,564 PointsHey 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!
Lauren Moineau
9,483 PointsLauren Moineau
9,483 PointsHi Matteo. Sorry to hear that :( Sure, I'll have a look later and let you know.
Lauren Moineau
9,483 PointsLauren Moineau
9,483 PointsHi Matteo. I've just had a look and there are 2 little things you need to fix:
In
AndroidManifest.xml
In the<data>
tag of the redirecting URL<intent-filer>
, you do not need to add "https://" to thehost
attribute as thescheme
attribute set right above it does that for you. Just amend thehost
attribute and you will be redirected properly to the app:In
MainActivity.java
You just forgot to add the code toonResponse()
. 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:Then it works perfectly :) Let me know how it goes