提交文件到Google云端硬盘 [英] Submitting file to Google drive

查看:68
本文介绍了提交文件到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:\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"

Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:\LocalServer\htdocs\pdf\quickstart.php on line 18

警告:fgets()希望参数1为资源,第18行的C:\ LocalServer \ htdocs \ pdf \ quickstart.php中给出的字符串

Warning: fgets() expects parameter 1 to be resource, string given in C:\LocalServer\htdocs\pdf\quickstart.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云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆