2017-03-10 61 views
0

试图从个人帐户使用谷歌/ apiclient列出我的文件,并不能成功。我的脚步Authless访问谷歌驱动器api

  1. 创建的服务帐户
  2. 启用域代表团
  3. 下载的按键(服务帐户,而不是客户代表)
  4. 使用谷歌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("[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(); 

作为回应,我得到:

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

回答

0

不能启用@ gmail.com域宽代表团(DWD)帐户,因为你是不是域gmail.com的所有者。 DWD仅适用于G Suite帐户。对于gmail.com帐户,您需要采取另一种方法。我强烈建议您通过this documentation了解更多详情。

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