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

查看:30
本文介绍了将文件提交到 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 云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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