PHP Google Drive API Integration login and listing files of users -


i have code , working problem working 1 person. if logged in not show login screen ever again user unless token expires.

index.php

<?php  session_start(); require_once __dir__ . '/vendor/autoload.php';  $client = new google_client(); $client->setauthconfig('client_id.json'); $client->addscope(google_service_drive::drive);  if (file_exists("credentials.json")) {   $access_token = (file_get_contents("credentials.json"));   $client->setaccesstoken($access_token);   //refresh token if it's expired.   if ($client->isaccesstokenexpired()) {     $client->fetchaccesstokenwithrefreshtoken($client->getrefreshtoken());     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 $file) {         $res['name'] = $file->getname();         $res['id'] = $file->getid();         $files[] = $res;     }     print_r($files); } } 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 list files other wise login user through callback file.

callback.php

<?php  session_start(); require_once __dir__ . '/vendor/autoload.php'; $client = new google_client(); $client->setauthconfigfile('client_id.json'); $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 {   $client->authenticate($_get['code']);   $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 wrong approach guess because stores 1 token , shows value. there way every person not logged in needs login see files list.

thanks.

you can try adding button sign out or create function automatically sign out user. stated in post, can use gapi.auth2.getauthinstance().signout() prevent automatic sign-in on site after has been called.

you can try demo site. in demo, user signed out when leave page shown in following code:

window.onbeforeunload = function(e){   gapi.auth2.getauthinstance().signout(); }; 

in answer in related post, there explanation preventing auto-signin. there events can use window close or navigate away trigger sign out method.

hope helps.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -