当前位置: 首页 > 知识库问答 >
问题:

无需登录就认证谷歌驱动器上传[重复]

充小云
2023-03-14

我的身份验证和上传代码如下:

    $client = new Google_Client();
    $client->setClientId('clientid');
     $client->setClientSecret('clientsecret');
     $client->setAccessType("offline");


$client->setRedirectUri('http://localhost:60/pathtofile.php');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

session_start();

if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $_SESSION['access_token'] = $client->getAccessToken();
    } else
        $client->setAccessToken($_SESSION['access_token']);

    $service = new Google_Service_Drive($client);

    //Insert a file
    $file = new Google_Service_Drive_DriveFile();
    //$file->setName(uniqid().'.zip');
    $file->setName($zipName . '.zip');
    $file->setDescription('Testing document ZIP backups');
    $file->setMimeType('application/zip');

    $data = file_get_contents($zipPath = $publicPath . '\\removedorgname\\backups\\' . $zipName . '.zip');

    $createdFile = $service->files->create($file, array(
          'data' => $data,
          'mimeType' => 'application/zip',
          'uploadType' => 'multipart'
        ));

    print_r($createdFile);

} else {
    $authUrl = $client->createAuthUrl();
    header('Location: ' . $authUrl);
    exit();
}

我面临的问题是,它要求我手动登录到谷歌账户。我研究了刷新令牌的概念,但不明白如何实现它。

共有1个答案

蒙墨竹
2023-03-14

我想我已经找到了解决问题的方法,这是正确还是不正确?

  1. 在google提示时登录(仅为第一次)

现在您有了访问令牌(该令牌将过期),您需要刷新令牌。

  $_SESSION['access_token'] = $client->getAccessToken();

    $token = $client->getAccessToken();
    var_dump($token;)
if ($client->isAccessTokenExpired()) {

     $client->fetchAccessTokenWithRefreshToken('removedtoken');

 }
 类似资料: