doujia2386 2015-07-16 18:11
浏览 98

Google云端硬盘:file_put_contents(token.json):无法打开流:权限被拒绝

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:

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

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

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);

?>
  • 写回答

1条回答 默认 最新

  • ds0678 2015-07-16 18:25
    关注

    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.

    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());
    
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站