This is my serivice.php file :
putenv('GOOGLE_APPLICATION_CREDENTIALS=service-account.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject('user@mydomain.com');
$client->setScopes('https://www.googleapis.com/auth/drive.file');
$service = new Google_Service_Drive($client);
//Create a new drive file
$file = new Google_Service_Drive_DriveFile();
$file->setName('example');
$file->setMimeType('application/vnd.google-apps.file');
$data = file_get_contents('exapmle.txt');
//Upload the file to google docs
$createdFile = $service->files->create($file, array(
'data' => $data,
'mimeType' => $mimeType,
'uploadType' => 'multipart'
));
I have service-account.json file in same working directory.
But i am getting this error :
PHP Fatal error: Uncaught Google_Service_Exception: {
"error": "unauthorized_client",
"error_description": "Client is unauthorized to retrieve access tokens using this method."
}
in \vendor\google\apiclient\src\Google\Http\REST.php:118
I have Enabled G Suite Domain-wide Delegation also.