Upload Anonymously5:43 with Jamie Huson
Add in an option for users to anonymously upload images, allowing users to upload without authentication.
With all this working, I wanna show you how you can set up multiple 0:00 APIs within a single app using Retrofit. 0:02 So you may have in your app, connecting to Twitter, Facebook, Imgur, multiple APIs. 0:06 And to do that, it's really easy. 0:13 So in this app as an example what we're gonna do is 0:15 we're gonna think of the Imgur API as kinda two different APIs and 0:18 that's the anonymous and the authenticated. 0:23 So we've been using the authenticated API thus far where the user has to sign in and 0:27 we upload images on their behalf. 0:32 The next part we want to do is add the ability to anonymously upload images and 0:34 it turns out it's a really straightforward and 0:39 easy because retrofit makes it really easy. 0:42 So first we need to do is add a new API and just like we did it for 0:45 our interface for authenticating off, we're gonna do the exact same thing. 0:50 We're gonna add a new interface for Anon. 0:55 And we only have one method that we want which is the uploadImage and 1:00 it turns out it's the exact same as our uploadImage from before. 1:05 It looks the exact same so I'll just go ahead and copy that over. 1:10 So if you're using completely different APIs like Twitter or Facebook, 1:18 you would just have a different interface with the names of each one of those. 1:22 So here we have the upload image to the exact same API end point, but 1:28 we're gonna do it anonymously. 1:33 The next part we add to the service. 1:34 So here we can just add a new method, and it actually looks 1:38 basically identical to this one that we've already done so let's go and copy that. 1:43 Put it in here and instead of off the API it's gonna be AnonApi and 1:50 it's 'gonna return back to the Anon interface instance. 1:55 Same base URL, same clien, Gson factory is all good. 2:03 The only difference here is, we're not gonna have the access token anymore. 2:09 The access token was only given to us when we signed the user in and, 2:14 we're not gonnas have the user sign in for this. 2:17 So instead, the documentation, which I've linked to in the teacher's notes, 2:22 says that we can still upload anonymously. 2:26 We just have to change the header for the authorization, 2:29 and that's we're gonnas do here. 2:32 So, instead of the Bearer, what we do is we just say our Client-ID. 2:34 And we kept track of that earlier, so let's go ahead and get it. 2:41 And that's the only difference between these two APIs. 2:48 One uses the Client-ID the other uses the access token. 2:52 So now we can go ahead and head back to our main activity and 2:58 add the ability to anonymously upload. 3:01 We already have a button for that uploadAnon, it's gonna go down there. 3:03 And here will just add a method like we did before, 3:09 this one is called uploadAnon and it's actually, 3:12 nearly identical to this other one because the API is pretty much the exact same. 3:15 So let's just copy this, paste it and change the name. 3:21 And it can indicate that it's Anon. 3:30 And then we just need to get the other APIs. 3:34 So get the AnonApi.. 3:36 It turns out it's the exact same parameters. 3:39 So that's all the same, already in the same file. 3:42 The only difference here is what it comes back, 3:46 when i'm gonna fetch the account images here because we're not signed in. 3:48 So instead of that what we wanna do is let's go ahead and 3:51 just go to this newly uploaded image and that has a URL. 3:54 So when this comes back we get the image object and we're going to 3:59 grab the URL which they call little link and then just direct the user to that. 4:04 So we can do that using an Intent. 4:09 So we'll say startActivity, new Intent, 4:11 Intent.Action_VIEW and we just need to parse the URI. 4:16 And we're gonna get that from our response.body, 4:23 data which is the image, link. 4:27 So if we successfully upload this image we should then mean directed to 4:34 the browser where that image resides with a URL. 4:38 So let's run this and we can see how the user would upload it anonymously an image. 4:44 So in this case we haven't signed in yet. 5:00 But we have the upload anonymous image option. 5:02 So let's go ahead and choose it. 5:05 And we can see that when I came back, we were able to fetch that URL and 5:12 go to the browser to show that image that we just uploaded. 5:17 Now that we've used Retrofit, 5:22 you can see how easy it is to add new API calls to your apps. 5:24 It handles the hard parts with it's easy syntax and friendly callback mechanism. 5:28 You can also see the power retrofit in ok HTTP 5:33 have to customize requests however you need. 5:36 I can't wait to see what kinds of apps you all come up with next. 5:40
You need to sign up for Treehouse in order to download course files.Sign up