2017-03-10 169 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瞭解更多詳情。

總之,我認爲沒有用戶的同意就不可能做到這一點。我希望這個信息幫助。