我在第79行的REST.php中有400个错误请求,实现了GMAIL Api PHP
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我在第79行的REST.php中有400个错误请求,实现了GMAIL Api PHP相关的知识,希望对你有一定的参考价值。
我在第79行的REST.php中获得400 Bad Request,实现了GMAIL Api PHP
我使用下面的代码来实现:
// Get the API client and construct the service object.
$client = new Google_Client();
$client->setDeveloperKey(API_KEY);
$client->setClientId(OAUTH_CLIENT_ID);
$client->setClientSecret(OAUTH_CLIENT_SECRET);
$client_email = MY_SERVICE_ACCOUNT_ID;
$private_key = file_get_contents('MYP12.p12');
$scopes = array(
'https://www.googleapis.com/auth/userinfo.email',
'https://mail.google.com'
);
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
$client->setApplicationName("Gmail Sample");
$client->setAssertionCredentials($credentials);
//Refresh the token if it's expired.
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$token = $client->getAccessToken();
$client->setAccessToken($token);
$service = new Google_Service_Gmail($client);
$user = 'me';
$date_last = date('Y-m-d');
$newdate = strtotime('-1 day', strtotime($date_last));
$newdate = date('Y-m-d', $newdate);
$q = " 'after:".$newdate."'";
$messagesResponse = $service->users_messages->listUsersMessages($user, ['maxResults'=>1000 , 'q'=> $q]);
echo '<pre>';
print_r($messagesResponse);exit;
致命错误:未捕获的异常“Google_Service_Exception”,并显示消息“错误呼叫GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults = 1000&q = +%27after%3A2017-12-26%27&key = [API_KEY]:第39行的C: wamp www google google_calendar src Google Http REST.php中的(400)错误请求
答案
您必须用您的凭据替换API_KEY,OAUTH_CLIENT_ID,OAUTH_CLIENT_SECRET,[MY_SERVICE_ACCOUNT_ID]。
$client->setDeveloperKey(API_KEY);
$client->setClientId(OAUTH_CLIENT_ID);
$client->setClientSecret(OAUTH_CLIENT_SECRET');
$client_email = '[MY_SERVICE_ACCOUNT_ID]';
以上是关于我在第79行的REST.php中有400个错误请求,实现了GMAIL Api PHP的主要内容,如果未能解决你的问题,请参考以下文章
为啥我在 AngularJs 帖子中收到 400 个错误请求?
我在 phpmyadmin 中有一个错误 #1064 :-#1064
当我使用“ValidateAntiForgeryToken”和“Authorize”时收到 400 个错误请求