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