dqwolwst50489 2017-03-10 16:00
浏览 34

Authless访问谷歌驱动器API

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("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();

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条回答 默认 最新

  • duanbushi1479 2017-03-11 17:48
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看