使用此代码来认证和上传测试文件。您需要
<YOUR_REGISTERED_REDIRECT_URI>对此文档本身进行设置(以及在控制台中)以进行身份验证。
require_once 'Google/Client.php';require_once 'Google/Service/Drive.php';$client = new Google_Client();// Get your credentials from the console$client->setClientId('<YOUR_CLIENT_ID>');$client->setClientSecret('<YOUR_CLIENT_SECRET>');$client->setRedirectUri('<YOUR_REGISTERED_REDIRECT_URI>');$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));session_start();if (isset($_GET['pre']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) { if (isset($_GET['pre'])) { $client->authenticate($_GET['pre']); $_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().'.jpg'); $file->setDescription('A test document'); $file->setMimeType('image/jpeg'); $data = file_get_contents('a.jpg'); $createdFile = $service->files->create($file, array( 'data' => $data, 'mimeType' => 'image/jpeg', 'uploadType' => 'multipart' )); print_r($createdFile);} else { $authUrl = $client->createAuthUrl(); header('Location: ' . $authUrl); exit();}


