Gmail API:尝试发送电子邮件时出现 400 个错误请求(PHP 代码)
我期望下面的代码发送电子邮件,但我只得到这个:
发生错误:调用 POST https://www.googleapis.com/gmail/v1/users/me/messages/send 时出错:(400) 错误的请求
我在底部得到了一个使用Google开发人员控制台。有什么帮助吗?200 OK
$client_id = '599901532082-js1r50n20q6n5mir9fo1g81qkj9kfn3j.apps.googleusercontent.com';
$service_account_name = '599901532082-js1r50n20q6n5mir9fo1g81qkj9kfn3j@developer.gserviceaccount.com';
$key_file_location = '/tmp/APIProject-cb6558ba6435.p12';
$client = new \Google_Client();
$client->setApplicationName("Client_Library_Examples");
$service = new \Google_Service_Gmail($client);
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new \Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/gmail.send', 'https://www.googleapis.com/auth/gmail.compose'),
$key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
//check if you want the validity of this string at: http://www.komeil.com/toolbox/base64decoder
//it is web safe base64 encoded email
$mime = "RnJvbTogSm9obiBEb2UgPHRpcmVuZ2FyZmlvQGdtYWlsLmVzPiANClRvOiBNYXJ5IFNtaXRoIDx0aXJlbmdhcmZpb0BnbWFpbC5jb20-IA0KU3ViamVjdDogU2F5aW5nIEhlbGxvIA0KRGF0ZTogRnJpLCAyMSBOb3YgMTk5NyAwOTo1NTowNiAtMDYwMCANCk1lc3NhZ2UtSUQ6IDwxMjM0QGxvY2FsLm1hY2hpbmUuZXhhbXBsZT4NCg0KVGhpcyBpcyBhIG1lc3NhZ2UganVzdCB0byBzYXkgaGVsbG8uIFNvLCAiSGVsbG8iLg==";
$service = new \Google_Service_Gmail($client);
$msg = new \Google_Service_Gmail_Message();
$msg->setRaw($mime);
try {
$results = $service->users_messages->send("me", $msg);
print 'Message with ID: ' . $message->getId() . ' sent.';
return $message;
} catch (\Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
编辑:
这是请求对象。它还包括响应数据:
object(Google_Http_Request)[508]
private 'batchHeaders' =>
array (size=3)
'Content-Type' => string 'application/http' (length=16)
'Content-Transfer-Encoding' => string 'binary' (length=6)
'MIME-Version' => string '1.0' (length=3)
protected 'queryParams' =>
array (size=0)
empty
protected 'requestMethod' => string 'POST' (length=4)
protected 'requestHeaders' =>
array (size=3)
'content-type' => string 'application/json; charset=UTF-8' (length=31)
'authorization' => string 'Bearer ya29.8gEUMiBLfxS8OLdSmpiQ-EcumeATo2qFAfPtPqwTw9fQ2zVrfZaA1X5OLoBmQccrXr8V8g' (length=82)
'accept-encoding' => string 'gzip' (length=4)
protected 'baseComponent' => string 'https://www.googleapis.com' (length=26)
protected 'path' => string '/gmail/v1/users/me/messages/send' (length=32)
protected 'postBody' => string '{"raw":"RnJvbTogSm9obiBEb2UgPHRpcmVuZ2FyZmlvQGdtYWlsLmVzPg0KVG86IE1hcnkgU21pdGggPHRpcmVuZ2FyZmlvQGdtYWlsLmNvbT4NClN1YmplY3Q6IFNheWluZyBIZWxsbw0KDQpUaGlzIGlzIGEgbWVzc2FnZSBqdXN0IHRvIHNheSBoZWxsby4gU28sICdIZWxsbycu"}' (length=214)
protected 'userAgent' => string 'Client_Library_Examples google-api-php-client/1.0.6-beta (gzip)' (length=63)
protected 'canGzip' => boolean true
protected 'responseHttpCode' => null
protected 'responseHeaders' => null
protected 'responseBody' => null
protected 'expectedClass' => string 'Google_Service_Gmail_Message' (length=28)
public 'accessKey' => null
object(Google_Http_Request)[508]
private 'batchHeaders' =>
array (size=3)
'Content-Type' => string 'application/http' (length=16)
'Content-Transfer-Encoding' => string 'binary' (length=6)
'MIME-Version' => string '1.0' (length=3)
protected 'queryParams' =>
array (size=0)
empty
protected 'requestMethod' => string 'POST' (length=4)
protected 'requestHeaders' =>
array (size=4)
'content-type' => string 'application/json; charset=UTF-8' (length=31)
'authorization' => string 'Bearer ya29.8gEUM***fxS8OLdSmpiQ-EcumeATo2qFAfPtPqwTw9fQ2zVrfZaA1X5OLoBmQccrXr8V8g' (length=82)
'accept-encoding' => string 'gzip' (length=4)
'content-length' => int 214
protected 'baseComponent' => string 'https://www.googleapis.com' (length=26)
protected 'path' => string '/gmail/v1/users/me/messages/send' (length=32)
protected 'postBody' => string '{"raw":"RnJvbTogSm9obiBEb2UgPHRpcmVuZ2FyZmlvQGdtYWlsLmVzPg0KVG86IE1hcnkgU21pdGggPHRpcmVuZ2FyZmlvQGdtYWlsLmNvbT4NClN1YmplY3Q6IFNheWluZyBIZWxsbw0KDQpUaGlzIGlzIGEgbWVzc2FnZSBqdXN0IHRvIHNheSBoZWxsby4gU28sICdIZWxsbycu"}' (length=214)
protected 'userAgent' => string 'Client_Library_Examples google-api-php-client/1.0.6-beta (gzip)' (length=63)
protected 'canGzip' => boolean true
protected 'responseHttpCode' => int 400
protected 'responseHeaders' =>
array (size=13)
'vary' => string 'Origin
X-Origin' (length=15)
'content-type' => string 'application/json; charset=UTF-8' (length=31)
'content-encoding' => string 'gzip' (length=4)
'date' => string 'Fri, 18 Sep 2015 08:34:25 GMT' (length=29)
'expires' => string 'Fri, 18 Sep 2015 08:34:25 GMT' (length=29)
'cache-control' => string 'private, max-age=0' (length=18)
'x-content-type-options' => string 'nosniff' (length=7)
'x-frame-options' => string 'SAMEORIGIN' (length=10)
'x-xss-protection' => string '1; mode=block' (length=13)
'server' => string 'GSE' (length=3)
'alternate-protocol' => string '443:quic,p=1' (length=12)
'alt-svc' => string 'quic=":443"; p="1"; ma=604800' (length=29)
'transfer-encoding' => string 'chunked' (length=7)
protected 'responseBody' => string '{
"error": {
"errors": [
{
"domain": "global",
"reason": "failedPrecondition",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
' (length=179)
protected 'expectedClass' => string 'Google_Service_Gmail_Message' (length=28)
public 'accessKey' => null