+91 9717203377

Home » Web » Refresh Oauth token for google APIs

Refresh Oauth token for google APIs

OAuth 2.0 is the next evolution of the OAuth protocol and it provide specific authorization flows for web applications, desktop applications, mobile phones, and living room devices.

Google use OAuth2.0 for authentication while accessing the Google APIs. We need access token from the Google Authorization Server to access data from google. Here are basic steps and code to develop a basic application for google API.

  1. Obtain Credentials i.e. client ID and client secret from the Google Developers Console.
  2. Download PHP SDK for google API Download from github
  3. Here is basic code for authentication of offline access of data. With this type of access we don’t need to login again and again.
    require_once 'Google/Client.php';
    require_once 'Google/Service/Drive.php';
    /*Database Connection*/
    $con = mysql_connect('localhost','root',''); //Set detail
    $client_id = 'Your CLIENT ID';
    $client_secret = 'PUT HERE';
    $redirect_uri = 'PUT URL of this page and set from console also';
    $scope = 'https://www.googleapis.com/auth/drive';
      Make an API request on behalf of a user. In
      this case we need to have a valid OAuth 2.0
      token for the user, so we need to send them
      through a login flow. To do this we need some
      information from our API console project.
    $client = new Google_Client();
    $client->setApprovalPrompt('force'); // This is for invoke the refresh_token otherwise remove line or set auto
  4. To avoid login each time we need to maintain access token for long time. This is basically refresh_token which authenticate from server automatically and make connect each time. It comes first time after authenticate in offline state.