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

Eric De Wildt
PLUS
Eric De Wildt
Courses Plus Student 13,077 Points

Missing Code

Jamie Huson Maybe I'm missing something but the code in onResume() in the video and the code in the project download is not the same. The onResume() in the download is empty accept for a //TODO. this makes the application un-runnable.

3 Answers

Jamie Huson
STAFF
Jamie Huson
Treehouse Guest Teacher

Eric De Wildt I reviewed the video entirely and it looks like some of the video editing made that section go by quick. Its at the 19 minute mark. Here's what the the code for onResume() should look like:

        Uri uri = getIntent().getData();
        if (uri != null && uri.toString().startsWith(Imgur.REDIRECT_URI)) {

            // create a temp Uri to make it easier to pull out the data we need
            Uri temp = Uri.parse("https://treehouseworkshop.com?" + uri.getFragment().trim());

            OAuthUtil.set(OAuthUtil.ACCESS_TOKEN, temp.getQueryParameter(OAuthUtil.ACCESS_TOKEN));
            OAuthUtil.set(OAuthUtil.EXPIRES_IN, System.currentTimeMillis() + (Long.parseLong(temp.getQueryParameter(OAuthUtil.EXPIRES_IN)) * 1000));
            OAuthUtil.set(OAuthUtil.TOKEN_TYPE, temp.getQueryParameter(OAuthUtil.TOKEN_TYPE));
            OAuthUtil.set(OAuthUtil.REFRESH_TOKEN, temp.getQueryParameter(OAuthUtil.REFRESH_TOKEN));
            OAuthUtil.set(OAuthUtil.ACCOUNT_USERNAME, temp.getQueryParameter(OAuthUtil.ACCOUNT_USERNAME));

            if (OAuthUtil.isAuthorized()) {
                toolbar.setTitle(OAuthUtil.get(OAuthUtil.ACCOUNT_USERNAME));
                showAccountImages();
            } else {
                // TODO later                
            }
        }
Marcelino Yax
Marcelino Yax
10,966 Points

Remember also to add the following code under case R.id.btn_sign_in: startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Imgur.AUTHORIZATION_URL)));

Trainer Workout
Trainer Workout
22,341 Points

When I click Sign in I get this error in logcat

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https//api.imgur.com/oauth2.authorize?client_id=ed16bd1ed461daf&response_type=token }

Do I have to add something to the manifest ?

Kevin Perez
PLUS
Kevin Perez
Courses Plus Student 8,180 Points

And don't forget

@Override public void onClick(View v) {

    switch (v.getId()) {
        case R.id.btn_upload_anon:
            // TODO
            break;
        case R.id.btn_upload:
            // TODO
            break;
        case R.id.btn_sign_in:
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(Imgur.AUTHORIZATION_URL)));
            break;
    }

}

Niki Izvorski
Niki Izvorski
43 Points

Here is the onResume but i still get a request without client_ID and response type are required and i am getting a failed on the request and its even opening in a browser.

@Override protected void onResume() { super.onResume();

    Uri uri = getIntent().getData();
    if (uri != null && uri.toString().startsWith(Imgur.REDIRECT_URL)) {

        // create a temp Uri to make it easier to pull out the data we need
        Uri temp = Uri.parse("https://treehouseworkshop.com?" + uri.getFragment().trim());

        OAuthUtil.set(OAuthUtil.ACCESS_TOKEN, temp.getQueryParameter(OAuthUtil.ACCESS_TOKEN));
        OAuthUtil.set(OAuthUtil.EXPIRES_IN, System.currentTimeMillis() + (Long.parseLong(temp.getQueryParameter(OAuthUtil.EXPIRES_IN)) * 1000));
        OAuthUtil.set(OAuthUtil.TOKEN_TYPE, temp.getQueryParameter(OAuthUtil.TOKEN_TYPE));
        OAuthUtil.set(OAuthUtil.REFRESH_TOKEN, temp.getQueryParameter(OAuthUtil.REFRESH_TOKEN));
        OAuthUtil.set(OAuthUtil.ACCOUNT_USERNAME, temp.getQueryParameter(OAuthUtil.ACCOUNT_USERNAME));

        if (OAuthUtil.isAuthorized()) {
            toolbar.setTitle(OAuthUtil.get(OAuthUtil.ACCOUNT_USERNAME));
            showAccountImages();
        } else {
            toolbar.setTitle("Login");
           showLoginOrAnon();
        }
    }
}

I found what went wrong i made changes to my Interface and to intent filter and all went fine after that

public interface Imgur {

String IMGUR_BASE_URL = "https://api.imgur.com";
String IMGUR_CLIENT_ID = "xxxxxxxxxxxxxxxxxxxx";
String AUTHORIZATION_URL = "https://api.imgur.com/oauth2/authorize?client_id=" + IMGUR_CLIENT_ID + "&response_type=token";
String REDIRECT_URL = "https://treehouseworkshop:88";


interface Auth {
    @GET("3/account/{username}/images/{page}")
    Call<Basic<ArrayList<Image>>> Images(@Path("username") String username,
                                        @Path("page") int page);
}

}

Make Sure your Redirect_URL in Imgur website is exactly "https://treehouseworkshop:88" and it will not respond but you will atleast be able to open it with your app and i get failed so far to get the images i will continue to try and update as soon as i get them.