不需要PHP Google Drive API身份验证


PHP Google Drive API authentication not required

我正在开发一个使用Google驱动器API的PHP代码。API可以在拥有该驱动器的Google帐户登录时上载文件。我的问题是,即使拥有它的谷歌帐户没有登录,它是否有可能上传文件,例如,没有登录谷歌帐户或其他谷歌帐户。我只想让驱动器公开,任何人都可以上传文件。

这是我使用的代码。

<?php
  function setGoogleDriveUpload($clientID, $secretKey, $redirecturi, $newfilename, $uploadfile){
    session_start();
    $url_array = explode('?', 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    $url = $url_array[0];
    require_once 'google-api-php-client/src/Google_Client.php';
    require_once 'google-api-php-client/src/contrib/Google_DriveService.php';   
    $client = new Google_Client();
    $client->setClientId($clientID);
    $client->setClientSecret($secretKey);
    $client->setRedirectUri($redirecturi);
    $client->setScopes(array('https://www.googleapis.com/auth/drive'));
    if (isset($_GET['code'])) {
       $_SESSION['accessToken'] = $client->authenticate($_GET['code']);
       header('location:'.$url);exit;
    } elseif (!isset($_SESSION['accessToken'])) {
       $client->authenticate();
    }
    $client->setAccessToken($_SESSION['accessToken']);
    $service = new Google_DriveService($client);
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $file = new Google_DriveFile();
    $file_path = $uploadfile;
    $mime_type = finfo_file($finfo, $file_path);
    $file->setTitle($newfilename);
    $file->setDescription('This is a '.$mime_type.' document');
    $file->setMimeType($mime_type);
    $service->files->insert(
        $file,
        array(
            'data' => file_get_contents($file_path),
            'mimeType' => $mime_type
        )
    );
    finfo_close($finfo);
}

"我的问题是,即使拥有它的谷歌帐户没有登录,它是否有可能上传文件"

是,前提是帐户所有者已授予脱机访问权限。在这种情况下,您的应用程序将收到一个刷新令牌,您可以存储该令牌,并在想要上传文件时使用该令牌生成访问令牌。