gpt4 book ai didi

php - 将文件提交到 Google Drive

转载 作者:行者123 更新时间:2023-12-04 04:33:21 26 4
gpt4 key购买 nike

我试图将文件提交到 Google Drive。虽然文件已上传到驱动器,但在点击接受按钮后出现错误。

<?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);
?>
错误信息:

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

Warning: fgets() expects parameter 1 to be resource, string given in C:\LocalServer\htdocs\pdf\quickstart.php on line 18


如何修复

最佳答案

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 上传/下载......

关于php - 将文件提交到 Google Drive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20183954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com