繁体   English   中英

对Google Drive API的无权访问

[英]Authless access to google drive api

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

  1. 创建服务帐号
  2. 启用域委托
  3. 下载了密钥(用于服务帐户,而不是客户委托)
  4. 使用Google API客户端进行连接(以下代码)

初始化和上市

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." } Uncaught Google_Service_Exception: { "error": "unauthorized_client", "error_description": "Client is unauthorized to retrieve access tokens using this method." }因此,主要问题是我缺少了什么? 在哪里可以预授权代理帐户? 还是有另一种无需用户确认即可与Drive Api通信的方法?

您不能为@ gmail.com帐户启用域范围内的授权(DWD),因为您不是域gmail.com的所有者。 DWD仅适用于G Suite帐户。 对于gmail.com帐户,您需要采取另一种方法。 我强烈建议您阅读本文档以获取更多详细信息。

总而言之,如果没有用户的同意,我认为不可能做到这一点。 我希望这个信息帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM