PHP Fatal error: Uncaught Google\Service\Exception: { "error": "unauthorized_client", "error_description": "Client is unauthorized to retrieve access tokens using t his method, or client not authorized for any of the scopes requested." } in C:\Sources\scripts\script-02\vendor\google\apiclient\src\Http\REST.php:128 Stack trace: #0 C:\Sources\scripts\script-02\vendor\google\apiclient\src\Http\REST.php(103): Google\Http\REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(Gu zzleHttp\Psr7\Request), false) #1 [internal function]: Google\Http\REST::doExecute(Object(GuzzleHttp\Client), O bject(GuzzleHttp\Psr7\Request), false) #2 C:\Sources\scripts\script-02\vendor\google\apiclient\src\Task\Runner.php(182) : call_user_func_array(Array, Array) #3 C:\Sources\scripts\script-02\vendor\google\apiclient\src\Http\REST.php(66): G oogle\Task\Runner->run() #4 C:\Sources\scripts\script-02\vendor\google\apiclient\src\Client.php(898): Goo gle\Http\REST::execute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request ), false, Array, in C:\Sources\scripts\script-02\vendor\google\apiclient\src\Ht tp\REST.php on line 128
<?php
require __DIR__ . '/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=key\credentials.json');
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google\Service\Drive::DRIVE);
$client->setSubject('[email protected]');
$service = new Google\Service\Drive($client);
DEFINE("TESTFILE", 'testfile.txt');
if (!file_exists(TESTFILE)) {
$fh = fopen(TESTFILE, 'w');
fseek($fh, 1024*1024*20);
fwrite($fh, "!", 1);
fclose($fh);
}
$file = new Google\Service\Drive\DriveFile();
$file->name = "Big File";
$chunkSizeBytes = 1 * 1024 * 1024;
// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->create($file);
// Create a media file upload to represent our upload process.
$media = new Google\Http\MediaFileUpload(
$client,
$request,
'text/plain',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize(TESTFILE));
// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen(TESTFILE, "rb");
while (!$status && !feof($handle)) {
// read until you get $chunkSizeBytes from TESTFILE
// fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
// An example of a read buffered file is when reading from a URL
$chunk = readVideoChunk($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
// The final value of $status will be the data from the API for the object
// that has been uploaded.
$result = false;
if ($status != false) {
$result = $status;
}
fclose($handle);
function readVideoChunk ($handle, $chunkSize)
{
$byteCount = 0;
$giantChunk = "";
while (!feof($handle)) {
// fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
$chunk = fread($handle, 8192);
$byteCount += strlen($chunk);
$giantChunk .= $chunk;
if ($byteCount >= $chunkSize)
{
return $giantChunk;
}
}
return $giantChunk;
}
I used this, and it's supposed to work from looking at the google results, but reading the documentation itself it seems that they're saying we need to use oAuth. If so, then how can we use the backend to allow anyone to upload to our Google Drive? I don't want someone to log through OAuth to upload files.
https://developers.google.com/drive/api/v3/about-auth
Is there a way to hardcode authentication so that it gets done automatically without the need to go to a link and log in?