Tutorial details

[TUT] Get your app OAuth YouTube API access | App Code for Sale | Preview

[TUT] Get your app OAuth YouTube API access | Android Tutorial

How to allow a user to sign into YouTube, getting your application authorised so you can use the YouTube API

Overview PAGE TOP

This tutorial will explain how to allow a user to sign into YouTube, getting your application authorised so you can use the YouTube API. This will allow you to upload / like / post comments etc for the YouTube user. This uses OAuth in the standard way i.e. requesting and receiving an access token.

References I’ve used for this tutorial are here:

YouTube API here.

Developers Guide OAuth2

Guide Installed Applications

Google API Console

First off you have to register a client with google API’s to get a client ID. This is explained here: Part 1. Once you’ve got a client ID we can get on with creating the app.

What we are going to do:

  • Allow a user to login to YouTube using a WebView
  • Capture the response from YouTube
  • Parse this response to gain an authorisation code
  • Do a Http Request to exchange the auth code for an access token

The access token is your golden key, this is what you use in all YouTube API requests that require a user to be logged in.

Here .. we .. go.

First off we just have a nice little intro activity, this has your sign in button for the user to sign in to YouTube. Clicking this button starts our login webview. It uses annotations to highlight the xml entry point, further explanation here. When signing in has finished we get the result, successful or not. If we have signed in to YouTube we print the result to LogCat. If the user has failed to sign in we start another activity explaining why they should sign in.


+ expand source

YouTube login in a WebView

After clicking sign in a new activity with a webview is presented. The webview is pretty lightweight for this tutorial and I haven’t even added a loading animation in between webpages so it may look like it isn’t doing anything but it is. Once the user is signed in it will then ask if they want to allow your app access to their YouTube account.


When either “allow access” or “no thanks” is clicked the webview is redirected to the url that is in your api console. In this tutorial it is set to “http://localhost” which is a pretty crap and non-unique name. If I was doing this again I’d pick something more unique. Perhaps even a real url on my server so a nice page is displayed (if only for a split second).

The difference between “allow access” and “no thanks” is what is appended after your redirect url. If they allow access it adds a code paramater, if they clicked no thanks an access_denied paramater is added. We capture these urls in our webviews client. This allows us to change the application flow for access or denial.


+ expand source


+ expand source


+ expand source

Creating a listener for when the redirect url is being loaded helps us keep to the single responsibility principle, the activity is kept cleaner and our webviewclient is lightweight. When the user selects “allow access” and the code paramater is added, we need to capture this code, it is used for authorisation later on. Capturing this code is easy as splitting it off the end of the url. This job is delegated to our ParamChecker class.


+ expand source

The param checker is also responsible for indicating if our app was allowed access or not. This uses a callback listener, our OAuthActivity is informed of the result.


+ expand source

When our OAuthActivity is told we have been refused access it loads another activity to tell the user our app can’t do much else if you deny access.

When our OAuthActivity is told we have been granted access (and is passed the auth code) we then start our final task to get the access token. Don’t forget the access token is our golden key, allowing us to perform YouTube API calls on behalf of the user. The final task is our GetTokensTask, this sends a request to the Google servers swapping our auth code for the access token. This is in the form of a POST request, you also have to add some other parameters to the POST request. These include your client id that you get from registering your app (see start of this post).


+ expand source

When you send the POST request to exchange your auth code for the access token, Google sends back JSON, this needs to be parsed to retrieve the access token. In this JSON is the access token, a refresh token and a expiry time. The refresh token is used if your access_token expires, you’ll use it to gain another (new) access token. We capture these tokens into a domain object, that way we can pass it between activities.


+ expand source


+ expand source

In this tutorial when you gain your access token it is just printed to your LogCat console. In your app you will want to store the access and refresh tokens (probably to sharedpreferences) you can then use it for your API calls.


YouTube AccessToken LogCat

Thats it! You have now been OAuth’d to manage your users YouTube account.

Any questions just ask.


Download YouTube SignIn Tutorial Eclipse Source Code

Go to GitHub YouTube OAuth Repo

Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.