Google云端硬盘:file_put_contents(token.json):无法打开流:权限被拒绝 [英] Google Drive: file_put_contents(token.json): failed to open stream: Permission denied

查看:102
本文介绍了Google云端硬盘:file_put_contents(token.json):无法打开流:权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此代码时,首先显示一个键为key ="4/XXXXXXX"的消息,然后将该键放入代码中,再次运行并显示此消息:

When I run this code firstly show a message with the key key="4/XXXXXXX", later put this key in the code, run again and appear this message:

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

Warning: file_put_contents(token.json): failed to open stream: Permission denied in /var/www/html/drive1/index8.php on line 17

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

Warning: file_get_contents(token.json): failed to open stream: Permission denied in /var/www/html/drive1/index8.php on line 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行

Fatal error: Uncaught exception 'Google_AuthException' with message 'Could not json decode the token' in /var/www/html/drive1/google-api-php-client/src/auth/Google_OAuth2.php:162 Stack trace: #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} thrown in /var/www/html/drive1/google-api-php-client/src/auth/Google_OAuth2.php on line 162

有人知道问题出在哪里吗?这是代码:

Does anyone know what the problem is? This is the code:

<?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.

The code is trying to write to the "token.json" file. But the server is preventing that because of unsufficient permissions. You should chmod the directory where this code is in to 777.

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

Maybe this website helps https://www.maketecheasier.com/file-permissions-what-does-chmod-777-means/

您还可以跳过写入文件的过程,并用以下内容替换这两行:

You can also skip the writing to the file and replace those two lines with this:

$drive->setAccessToken($drive->authenticate());

这篇关于Google云端硬盘:file_put_contents(token.json):无法打开流:权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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