TenG
TenG

Reputation: 4004

Google Drive OAuth2 - confused about callback and redirect URI

The scenario is pretty straightforward. A web app is being developed which will allow the user to access their Google Drive to save/read files.

Using the PHP quickstart example from Google, I am able to get it to work whereby it read the client secret info from the Google generated JSON file, prints the requester URL, copy paste that into a browser, which invokes the Google permissions page, and then we get redirected to http://localhost with the code as a GET parameter. Copy/past the code into the quickstart demo app and it works.

However the user experience, especially on a tablet/phone is awful.

I would prefer to be able to redirect to a server where it saves the code, and the invoking user web app session can then poll this server (with additional security credentials) to get the code.

What I cannot get my head around is how to get the callback/redirect URI that Google calls with the access code to include an additional identifier supplied by our invoking web app so that it can identify the correct response from Google in the case where we would have multiple users requesting the code at the same time.

Upvotes: 0

Views: 540

Answers (1)

Emel
Emel

Reputation: 2452

You can use the structure shown in Google OAuth2 for Server-side Web Apps. It helps you to handle the Authentication Flow without using the code strategy.

The main point is exchange the authorization token with an access one:

$client->authenticate($_GET[‘code’])
// Using getAccesToken method
$access_token = $client->getAccessToken();

You can take the example of PHP as a guide

index.php
<?php
require_once __DIR__.'/vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
  $drive = new Google_Service_Drive($client);
  $files = $drive->files->listFiles(array())->getItems();
  echo json_encode($files);
} else {
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
oauth2callback.php
<?php
require_once __DIR__.'/vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);

if (! isset($_GET['code'])) {
  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

Upvotes: 1

Related Questions