Automatically refresh token using google drive api with php script

后端 未结 2 1376
我在风中等你
我在风中等你 2020-12-01 08:33

I followed again THIS TUTORIAL to upload a file on Google Drive with php, directly from my REMOTE SERVER: so I have created new API Project from Google API Console, enabled

2条回答
  •  天命终不由人
    2020-12-01 09:32

    After messing a lot I got this to work. I am using one file/script to get the offline token and then a class to do stuff with the api:

    require_once 'src/Google/autoload.php'; // load library
    
    session_start();
    
    $client = new Google_Client();
    // Get your credentials from the console
    $client->setApplicationName("Get Token");
    $client->setClientId('...');
    $client->setClientSecret('...');
    $client->setRedirectUri('...'); // self redirect
    $client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
    $client->setAccessType("offline");
    $client->setApprovalPrompt('force'); 
    
    
    
    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $_SESSION['token'] = $client->getAccessToken();
        $client->getAccessToken(["refreshToken"]);
        $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
        return;
    }
    
    if (isset($_SESSION['token'])) {
        $client->setAccessToken($_SESSION['token']);
    }
    
    if (isset($_REQUEST['logout'])) {
        unset($_SESSION['token']);
        $client->revokeToken();
    }
    
    
    ?>
    
    
        
        
            

    Get Token

    getAccessToken()) { $_SESSION['token'] = $client->getAccessToken(); $token = json_decode($_SESSION['token']); echo "Access Token = " . $token->access_token . '
    '; echo "Refresh Token = " . $token->refresh_token . '
    '; echo "Token type = " . $token->token_type . '
    '; echo "Expires in = " . $token->expires_in . '
    '; echo "Created = " . $token->created . '
    '; echo "Logout"; file_put_contents("token.txt",$token->refresh_token); // saving access token to file for future use } else { $authUrl = $client->createAuthUrl(); print ""; } ?>

    You can load refresh token from file and use it as necessary for offline access:

    class gdrive{
    
    function __construct(){
            require_once 'src/Google/autoload.php';
            $this->client = new Google_Client();
    }
    
    function initialize(){
            echo "initializing class\n";
            $client = $this->client;
            // credentials from google console
            $client->setClientId('...');
            $client->setClientSecret('...');
            $client->setRedirectUri('...');
    
            $refreshToken = file_get_contents(__DIR__ . "/token.txt"); // load previously saved token
            $client->refreshToken($refreshToken);
            $tokens = $client->getAccessToken();
            $client->setAccessToken($tokens);
    
            $this->doSomething(); // go do something with the api       
        }
    }
    

    More here: https://github.com/yannisg/Google-Drive-Uploader-PHP

提交回复
热议问题