Search code examples
objective-cyoutube-apioauth-2.0xcode4.5access-token

How to get a new access token after expiration using OAuth 2.0 along with Youtube API in iOS application


I am using the Youtube Api in my iPad application. i managed to get authenticated and get the access token using the OAuth 2.0. my problem is that the token expires after one hour and i don't know how to get a new one using the refresh-token without going through the authentication process again. i am using XCode 4.5 and iOS 5.1 & 6


Solution

  • According do the documentation

    If your application obtains a refresh token during the authorization process, then you will need to periodically use that token to obtain a new, valid access token. Server-side web applications, installed applications, and devices all obtain refresh tokens.

    So if you already have your refresh token, you just need to perform a POST request configured as follows

    POST /o/oauth2/token HTTP/1.1
    Host: accounts.google.com
    Content-Type: application/x-www-form-urlencoded
    
    client_id=21302922996.apps.googleusercontent.com&
    client_secret=<YOUR CLIENT SECRET>
    refresh_token=<YOUR REFRESH TOKEN>
    grant_type=refresh_token
    

    and you'll get back a response like

    {
      "access_token":<A NEW ACCESS TOKEN>,
      "expires_in":<AN EXPIRING TIME>,
      "token_type":"Bearer"
    }