douzi1991 2015-03-21 20:38
浏览 56
已采纳

Google Directory API PHP客户端SDK出现{“error”:“invalid_grant”}错误

So I am trying all day already and I just can't seem to correctly authenticate to the google API. This is what I did to set up a connection so far:

  1. I first created a service account for my application
  2. Then I added that service account in the third party client access settings on the admin page for our Google Apps domain. I added the scopes for users and groups
  3. I generated a new Client ID for web applications
  4. I downloaded the .p12 file, the secret JSON file for my web Client ID and stored them locally

So I think that's all I need to succesfully authenticate.. I then used the following code to set everything up:

$this->client = new Google_Client();
$this->client->setAuthConfigFile(STORAGE_PATH.'client_secrets.json');
$this->client->addScope(static::$scopes);

$cred = new Google_Auth_AssertionCredentials(
    static::$service_account_email,
    static::$scopes,
    file_get_contents(STORAGE_PATH.'TRICS-key.p12'));
$cred->sub = static::$delegated_admin;

$this->client->setAssertionCredentials($cred);

$this->directory_service = new Google_Service_Directory($this->client);

Does someone know if I am forgetting something?

  • 写回答

1条回答 默认 最新

  • doupeng3501 2015-03-23 21:20
    关注

    Oops. I was a fool. I accidentally used the email of the Web Client instead of the service account email. :') The rest of the code seems to function as it should.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择