0
votes

Trying to list my files from personal account using google/apiclient and cannot succeed. My steps

  1. Created service account
  2. Enabled domain delegation
  3. Downloaded the keys (for the service account, not the client delegate)
  4. Using google api client to connect (code below)

Initialisation and listing

private function _initClient(string $keyLocation)
{
    if (empty($keyLocation) || !file_exists($keyLocation))
    {
        throw new \Exception("Missing google certificate file");
    }

    $client = new \Google_Client();
    $client->setApplicationName('My App');
    $client->useApplicationDefaultCredentials();
    $client->setSubject("[email protected]");
    $client->setScopes([
        'https://www.googleapis.com/auth/drive',
    ]);
    return $client;
}

public function listDirectories()
{
    $drive = new \Google_Service_Drive($this->client);
    $files = $drive->files->listFiles([
        'corpus' => 'user',
        'spaces' => 'drive'
    ]);


    var_dump($files);

}


require_once 'vendor/autoload.php';
$key = __DIR__.DIRECTORY_SEPARATOR.'client_id.json';
putenv('GOOGLE_APPLICATION_CREDENTIALS='.$key);
$t = new Myclass($key);
$t->listDirectories();

In response I get :

Uncaught Google_Service_Exception: { "error": "unauthorized_client", "error_description": "Client is unauthorized to retrieve access tokens using this method." } So the main question is what I am missing? where I can pre-authorized my delegated account ? Or there is another way to communicate with Drive Api without user confirmation?

1

1 Answers

0
votes

You cannot enable domain wide delegation(DWD) for @gmail.com accounts because you are not the owner of the domain gmail.com. DWD is only possible for G Suite accounts. For gmail.com accounts, you need to take another approach. I strongly recommend you to go over this documentation for more details.

In summary, I don't think is possible to do this without user consent. I hope this information helps.