我试图将文件提交到Google云端硬盘。虽然文件上传到驱动器,但是在按下接受按钮后我收到错误。
<?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 console
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('http://localhost/pdf/quickstart.php');
$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);
?>
错误讯息:
注意:使用未定义的常量STDIN - 在第18行的C:\ LocalServer \ htdocs \ pdf \ quickstart.php中假设为“STDIN”
警告:fgets()要求参数1为资源,字符串在第18行的C:\ LocalServer \ htdocs \ pdf \ quickstart.php中给出
如何修复
答案 0 :(得分:3)
STDIN是一个命令行指令,它不是一个直接有效的php语句。 我做的是:
$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('**********************');
$client->setClientSecret('*********');
$client->setRedirectUri('*********');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$authUrl = $client->createAuthUrl();
print $authurl
$authCode = ''; // insert the verification code that you get after going to url in $authurl
file_put_contents('token.json', $client->authenticate($authCode));
执行这么多后,您将在json文件token.json中获取您的身份验证信息...并且您可以使用以下内容上传...:
$service = new Google_DriveService($client);
$client->setAccessToken(file_get_contents('token.json'));
//Insert a file
..... // rest the same
一旦你让你的json再次运行顶级代码......每次只需使用token.json上传/下载....