使用服务帐户向Google API进行身份验证时发生failedPrecondition错误 [英] failedPrecondition error when authenticating with service account to Google API

查看:399
本文介绍了使用服务帐户向Google API进行身份验证时发生failedPrecondition错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Google API 2.0.0 RC4 for PHP从我自己的收件箱(作为项目所有者)获取给定的电子邮件。



我有一个项目设置,以JSON格式下载的身份验证文件,并且我已经在Google Developers Console中启用了gmail API。



但是当我运行下面的代码时,我得到一个Bad请求带有原因的错误failedPrecondition。现在,这让我相信访问权限有问题,但我无法弄清楚我需要在这里做什么。



代码:

 <?php 
require_once(google-api-php-client-2.0.0-RC4 / vendor / autoload。 PHP的);

$ client = new Google_Client();

//设置将要使用的范围
$ client-> setScopes([https://www.googleapis.com/auth/gmail.readonly] );

$ client-> setApplicationName(MyProject);
//使用2.0样式设置授权配置
$ client-> setAuthConfigFile(myAuthFile.json);

$ gmailHandle =新Google_Service_Gmail($ client);

$ messages = $ gmailHandle-> users_messages-> get(myemail@mydomain.com,12343445asd56);

echo json_encode($ messages);

?>

完整的错误如下所示:



< blockquote>

致命错误:带有消息'{错误的未捕获异常'Google_Service_Exception':{errors:[{domain:global,reason :
failedPrecondition,message:Bad Request}],code:400,
message:Bad Request}}'



解决方案

我只是遇到了这个问题,终于找到了解决方案。由于您使用的是服务帐户,但未指定主题作为配置的一部分,因此会出现该错误。这是API需要知道您在使用服务帐户时所处的电子邮件地址。您可以在Google_Client :: createApplicationDefaultCredentials中看到它尝试使用此主题。



解决方法是在调用setAuthConfig()之后在代码中添加以下行

  $ client-> setConfig('subject','you@email.com'); 

当您现在从收件箱中取出邮件时,请务必使用关键字'me'充当您配置为主题的电子邮件地址。

I'm trying to fetch a given email from my own inbox (as project owner) using the Google API 2.0.0 RC4 for PHP.

I have a project setup, a downloaded authentication file in JSON format and I have enabled the gmail API in the Google Developers Console.

But when I run the following code I get a "Bad Request" error with reason "failedPrecondition". Now this leads me to believe that there is something wrong with the access rights, but I can't figure out what I need to do here.

Code:

<?php
    require_once("google-api-php-client-2.0.0-RC4/vendor/autoload.php");

    $client = new Google_Client();

    // set the scope(s) that will be used
    $client->setScopes(["https://www.googleapis.com/auth/gmail.readonly"]);

    $client->setApplicationName("MyProject");
    // set the authorization configuration using the 2.0 style
    $client->setAuthConfigFile("myAuthFile.json");

    $gmailHandle = new Google_Service_Gmail($client);

    $messages = $gmailHandle->users_messages->get("myemail@mydomain.com", "12343445asd56");

    echo json_encode($messages);

?>

And the complete error looks like this:

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "errors": [ { "domain": "global", "reason": "failedPrecondition", "message": "Bad Request" } ], "code": 400, "message": "Bad Request" } } '

解决方案

I just ran into this and finally found the solution. You are getting that error because you are using a service account without specifying a "subject" as part of the configuration. That is required for the API to know which email address you are acting as when using the service account. You can see it attempting to use this subject in Google_Client::createApplicationDefaultCredentials

The solution is to add the following line in the code after you have called setAuthConfig()

$client->setConfig('subject', 'you@email.com');

Be sure to then use the keyword 'me' when fetching the messages from the inbox as you are now acting as the email address which you have configured as 'subject'.

这篇关于使用服务帐户向Google API进行身份验证时发生failedPrecondition错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆