Submitting file to Google drive(将文件提交到 Google 云端硬盘)
问题描述
我试图将文件提交到 Google 云端硬盘.虽然文件已上传到驱动器,但点击接受按钮后出现错误.
I was trying to submit file to Google Drive. Though the file is uploaded to drive, I am getting error after hitting accept button.
<?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:
$authUrl
";
print "Please enter the auth code:
";
$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:LocalServerhtdocspdfquickstart.php 中假定为STDIN"
Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:LocalServerhtdocspdfquickstart.php on line 18
警告:fgets() 期望参数 1 是资源,字符串在第 18 行的 C:LocalServerhtdocspdfquickstart.php 中给出
Warning: fgets() expects parameter 1 to be resource, string given in C:LocalServerhtdocspdfquickstart.php on line 18
如何解决
推荐答案
STDIN 是命令行指令,不是直接有效的 php 语句..我所做的是:
STDIN is a command line instruction and it isn't a direct valid php statement .. What i did was :
$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 中获得您的身份验证信息...您可以使用以下内容上传...:
After executing this much you'll get your authentication info in the json file token.json ... And you can use the following to upload ... :
$service = new Google_DriveService($client);
$client->setAccessToken(file_get_contents('token.json'));
//Insert a file
..... // rest the same
一旦你得到你的 json,就不要再运行顶级代码了......每次都使用 token.json 来上传/下载......
Once you get your json don't run the top codes again ... Just use the token.json everytime to upload/download ....
这篇关于将文件提交到 Google 云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将文件提交到 Google 云端硬盘
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01