Google Drive:file_put_contents(token.json):无法打开流:权限被拒绝

2024-03-21

当我运行此代码时,首先显示一条带有密钥 key="4/XXXXXXX" 的消息,稍后将此密钥放入代码中,再次运行并显示此消息:

警告:file_put_contents(token.json):无法打开流: /var/www/html/drive1/index8.php 第 17 行的权限被拒绝

警告:file_get_contents(token.json):无法打开流: /var/www/html/drive1/index8.php 第 19 行的权限被拒绝

致命错误:未捕获异常“Google_AuthException”并带有消息 '无法 json 解码令牌' /var/www/html/drive1/google-api-php-client/src/auth/Google_OAuth2.php:162 堆栈跟踪:#0 /var/www/html/drive1/google-api-php-client/src/Google_Client.php(174): Google_OAuth2->setAccessToken(NULL) #1 /var/www/html/drive1/index8.php(19): Google_Client->setAccessToken(false) #2 {main} 抛出 /var/www/html/drive1/google-api-php-client/src/auth/Google_OAuth2.php 162号线

有谁知道问题是什么?这是代码:

<?php

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$drive = new Google_Client();

$drive->setClientId('XXXXX');
$drive->setClientSecret('XXXXXX');
$drive->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$drive->setScopes(array('https://www.googleapis.com/auth/drive'));

$gdrive = new Google_DriveService($drive);

$_GET['code']= '4/XXXXX';

file_put_contents('token.json', $drive->authenticate());

$drive->setAccessToken(file_get_contents('token.json'));

$doc = new Google_DriveFile();

$doc->setTitle('Test Drive');
$doc->setDescription('Document');
$doc->setMimeType('text/plain');

$content = file_get_contents('drive.txt');

$output = $gdrive->files->insert($doc, array(
      'data' => $content,
      'mimeType' => 'text/plain',
    ));

print_r($output);

?>

该代码正在尝试写入“token.json”文件。但由于权限不足,服务器正在阻止这种情况。您应该将此代码所在的目录 chmod 为 777。

也许这个网站有帮助https://www.maketecheasier.com/file-permissions-what-does-chmod-777-means/ https://www.maketecheasier.com/file-permissions-what-does-chmod-777-means/

您还可以跳过写入文件并将这两行替换为:

$drive->setAccessToken($drive->authenticate());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Drive:file_put_contents(token.json):无法打开流:权限被拒绝 的相关文章

随机推荐