我正在使用PHP测试Google Drive API,但尚未设法通过命令行在我的驱动器上获取我的文档文件。我还尝试使用提供的PHP脚本创建一个文件夹。
我的PHP脚本运行并请求我从其请求我访问的URL获取的身份验证代码。在按下输入时,它看起来像它工作了一段时间然后它终止没有错误。当我转到“我的Google云端硬盘”时,我看不到任何新文件或文件夹
(使用OSX MAMP和PHP)
THX
使用的PHP代码
<?php
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();
// Get your credentials from the APIs Console
$client->setClientId('clien_id');
$client->setClientSecret('Secret');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);
$authUrl = $client->createAuthUrl();
//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));
// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);
//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$data = file_get_contents('document.txt');
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'text/plain',
));
print_r($createdFile);
?>