Authless访问谷歌驱动器api

时间:2017-03-10 16:00:12

标签: php google-api google-api-php-client

尝试使用google / apiclient从个人帐户列出我的文件,但无法成功。我的步骤

  1. 创建服务帐户
  2. 启用域名委派
  3. 下载密钥(对于服务帐户,而不是客户委托)
  4. 使用google api客户端进行连接(代码如下)
  5. 初始化和列表

    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("my.email@gmail.com");
        $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();
    

    作为回应,我得到了:

    Uncaught Google_Service_Exception: { "error": "unauthorized_client", "error_description": "Client is unauthorized to retrieve access tokens using this method." } 所以主要问题是我缺少什么?我可以在哪里预先授权我的委托帐户?或者有其他方式与Drive Api通信而无需用户确认?

1 个答案:

答案 0 :(得分:0)

您无法为 @ gmail.com 帐户启用域范围授权(DWD),因为您不是域 gmail.com 的所有者。 DWD仅适用于G Suite帐户。对于 gmail.com 帐户,您需要采取其他方法。我强烈建议您查看this documentation以了解更多详情。

总之,如果没有用户同意,我认为不可能这样做。我希望这些信息有所帮助。

相关问题