用PHP在Gmail API上收到400错误的请求 [英] 400 Bad Request on Gmail API with php

查看:88
本文介绍了用PHP在Gmail API上收到400错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的服务器使用php代码访问一个Gmail帐户.以下是我在网上可以找到的示例所创建的内容.

I want my server to access a gmail account with php code. And the following is what I created with the examples I can find online.

<?php

session_start();

require_once dirname(dirname(__FILE__)) . '/utility/google-api-php-client/src/Google/autoload.php';

$json_file_location = 'Gmail-YQXB-7c754a18211a.json';

$client = new Google_Client();
$service = new Google_Service_Books($client);
$sgmail = new Google_Service_Gmail($client);

$scopes = array('https://www.googleapis.com/auth/books', 'https://mail.google.com');

if (isset($_SESSION['service_token'])) {
    $client->setAccessToken($_SESSION['service_token']);
}

$cred = $client->loadServiceAccountJson($json_file_location, $scopes);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}

$_SESSION['service_token'] = $client->getAccessToken();

$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
echo "<h3>Results Of Call:</h3>";
foreach ($results as $item) {
    echo $item['volumeInfo']['title'], "<br /> \r\n";
}

$msg = $sgmail->users_messages->listUsersMessages('me');
var_dump($msg); 

?>

除最后一个请求外,该代码有效,该请求将抛出类似以下内容的

The code works except the last request, which will throw something like:

PHP致命错误:未捕获的异常'Google_Service_Exception'与 消息调用GET时出错 https://www.googleapis.com/gmail/v1/users/me/messages :(400)错误 请求中 C:\ inetpub \ wwwroot \ utility \ google-api-php-client \ src \ Google \ Http \ REST.php:110

PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/gmail/v1/users/me/messages: (400) Bad Request' in C:\inetpub\wwwroot\utility\google-api-php-client\src\Google\Http\REST.php:110

$msg = $sgmail->users_messages->listUsersMessages('me');

此帐户是服务帐户.如果使用Web应用程序类型客户端ID",则可以使其正常工作.只是不知道这里出了什么问题.

This account is a Service Account. I could make it working if I use a Web application type Client ID. Just don't know what is wrong here.

推荐答案

如果您只想访问一个帐户(您自己的帐户),则不应使用服务帐户.服务帐户是他们自己的帐户,不是Gmail帐户.它们适用于不需要用户的API(例如地图,搜索),或者当您使用Google Apps for Work域并希望为域中的所有用户启用委派时(通过域管理员),因此您不需要个人用户授权).

You should not be using a service account if you just want to access one account (your own). Service accounts are their own account and they're not Gmail accounts. They work well for APIs that don't need a user (e.g. maps, search) or when you are using a Google Apps for Work domain and want delegation enabled for all users in the domain (by domain admin, so you don't need individual user authorization).

您要使用标准的oauth2 Web身份验证流程.如果要确保它可以脱机使用(例如通过某些脚本/服务/cron作业),请确保在oauth2授权Web流请求上添加脱机标志,并确保获得永久刷新令牌(可以是用于请求访问令牌).如果您不设置为离线状态,那么它是一个访问令牌,只能使用一个小时.请参见 https://developers.google.com/gmail/api/auth/web-服务器以获取更多详细信息.

You want to use standard oauth2 web auth flow. If you want to make sure it's usable offline (e.g. by some script/service/cron job) then make sure you add the offline flag on your oauth2 authorization web flow request and that'll make sure you get a permanent refresh token (can be used to request an access token). If you don't set offline, then it's an access token which is only good for an hour. See https://developers.google.com/gmail/api/auth/web-server for more details.

这篇关于用PHP在Gmail API上收到400错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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