PHP Google Drive API Integration login and listing files of users


I have a code it and it is working problem is its only working for 1 person. So if I logged in then it will not show login screen ever again for any user unless that token expires.



require_once __DIR__ . '/vendor/autoload.php';

$client = new Google_Client();

if (file_exists("credentials.json")) {
  $access_token = (file_get_contents("credentials.json"));
  //Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
    file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
  $drive_service = new Google_Service_Drive($client);
  $files_list = $drive_service->files->listFiles(array())->getFiles(); 
  if (count($files_list) == 0) {
    print "No files found.\n";
} else {
    foreach ($files_list as $file) {
        $res['name'] = $file->getName();
        $res['id'] = $file->getId();
        $files[] = $res;
} else {
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));

So index file checks if credentials.json files has access token or not if exists then list files other wise login user through callback file.


require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/callback.php');
$client->addScope(Google_Service_Drive::DRIVE); //::DRIVE_METADATA_READONLY

if (! isset($_GET['code'])) {
  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
  $access_token = $client->getAccessToken();
  file_put_contents("credentials.json", json_encode($access_token));

  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/index.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));

This is the wrong approach I guess because it only stores 1 token and shows its value. Is there way that every person who is not logged in needs to login to see his files list.





You can try adding a button for sign out or create a function that will automatically sign out the user. As stated in this post, you can usegapi.auth2.getAuthInstance().signOut() to prevent automatic sign-in on your site after it has been called.

You can try his demo site. In the demo, the user is signed out when they leave the page as shown in the following code:

window.onbeforeunload = function(e){

In his answer in the related post, there are explanation about preventing auto-signin. There are events you can use like window close or navigate away to trigger the sign out method.

Hope this helps.

People are also looking for solutions to the problem: php - Unable to skip ranking Post when there is no ranking information in CakePHP2


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.