PHP Gmail API在编写时会抛出错误403

时间:2015-02-22 08:34:04

标签: php gmail-api

我已对Google用户进行身份验证并对身份验证令牌进行了编码。有了这个标记,我试图撰写电子邮件,但收到错误。

Error calling POST https://www.googleapis.com/gmail/v1/users/me/messages/send: (403) Insufficient Permission 

这是我的代码:

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/gmail.compose");
$service = new Google_Service_Gmail($client);

设置凭证并成功完成oauth后,

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $authUrl = $client->createAuthUrl();
}

if ($client->getAccessToken() && isset($_POST['message'])) {

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

// Prepare the message in message/rfc822
try {

    // The message needs to be encoded in Base64URL
    $mime = rtrim(strtr(base64_encode($_POST["message"]), '+/', '-_'), '=');
    $msg = new Google_Service_Gmail_Message();
    $msg->setRaw($mime);
    $service->users_messages->send("me", $msg);

 } catch (Exception $e) {
    print($e->getMessage());
 }

0 个答案:

没有答案