Google Drive Api V3 File Copy Not Working - PHP
I'm trying to copy file from one drive to another. It works, if i use their official API console in the documentation but it fails with the following code.
<?php
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/Google/Service.php';
require_once 'google-api-php-client/vendor/autoload.php';
$clientId = 'myClientId';
$clientSecret = 'mySecret';
$redirectUri = 'redirectUri';
$client = new Google_Client();
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);
$client->setAccessType('offline');
$refreshToken = file_get_contents(__DIR__ . "/token.txt");
$client->refreshToken($refreshToken);
$tokens = $client->getAccessToken();
$client->setAccessToken($tokens);
$client->setDefer(true);
$service = new Google_Service_Drive($client);
$fileId = "DemoFileID"; // Google File ID
$file = new Google_Service_Drive_DriveFile();
$request = $service->files->copy($fileId, $file);
print_r($request);
I get the following error:
GuzzleHttp\Psr7\Request Object ( [method:GuzzleHttp\Psr7\Request:private] => POST
[requestTarget:GuzzleHttp\Psr7\Request:private] => [uri:GuzzleHttp\Psr7\Request:private] => GuzzleHttp\Psr7\Uri Object ( [scheme:GuzzleHttp\Psr7\Uri:private] => https [userInfo:GuzzleHttp\Psr7\Uri:private] => [host:GuzzleHttp\Psr7\Uri:private] => www.googleapis.com
[port:GuzzleHttp\Psr7\Uri:private] => [path:GuzzleHttp\Psr7\Uri:private] => /drive/v3/files/DemoFileID/copy [query:GuzzleHttp\Psr7\Uri:private] => [fragment:GuzzleHttp\Psr7\Uri:private] => )
[headers:GuzzleHttp\Psr7\Request:private] => Array ( [Host] => Array ( [0] => www.googleapis.com ) [content-type] => Array ( [0] => application/json ) [X-Php-Expected-Class] => Array ( [0] => Google_Service_Drive_DriveFile ) )
[headerNames:GuzzleHttp\Psr7\Request:private] => Array ( [content-type] => content-type [host] => Host [x-php-expected-class] => X-Php-Expected-Class ) [protocol:GuzzleHttp\Psr7\Request:private] => 1.1 [stream:GuzzleHttp\Psr7\Request:private] => )
Answer
Solution:
Works after commenting the line
$client->setDefer(true);
if anyone encounter same message like above. just change
$client->setDefer(true);
to
//$client->setDefer(true);
Source