PHP - 使用Google Gmail OAuth2 API的ListMessages

时间:2016-02-09 13:11:42

标签: php email gmail-api google-oauth2

我正在创建一个基于网络的邮件客户端,使用Google OAuth2 API支持Google帐户,登录工作正常,Gmail首次登录时要求权限,但我不明白如何从用户帐户收到邮件。根据{{​​3}},我正在创建一个使用这些API的类,但我不明白如何使用它..

class Gmail
{
/*
 * Get list of Messages in user's mailbox.
 * @param  Google_Service_Gmail $service Authorized Gmail API instance.
 * @param  string $userId User's email address. The special value 'me'
 * can be used to indicate the authenticated user.
 * @return array Array of Messages.
 */
    function listMessages($service, $userId) {
        $pageToken = NULL;
        $messages = array();
        $opt_param = array();
        do {
            try {
                if ($pageToken) {
                    $opt_param['pageToken'] = $pageToken;
                }
                $messagesResponse = $service->users_messages->listUsersMessages($userId, $opt_param);
                if ($messagesResponse->getMessages()) {
                    $messages = array_merge($messages, $messagesResponse->getMessages());
                    $pageToken = $messagesResponse->getNextPageToken();
                }
            } catch (Exception $e) {
                print 'An error occurred: ' . $e->getMessage();
            }
        } while ($pageToken);

        foreach ($messages as $message) {
            print 'Message with ID: ' . $message->getId() . '<br/>';
        }

        return $messages;
    }

    function getMessage($service, $userId, $messageId) {
        try {
            $message = $service->users_messages->get($userId, $messageId);
            print 'Message with ID: ' . $message->getId() . ' retrieved.';
            return $message;
        } catch (Exception $e) {
            print 'An error occurred: ' . $e->getMessage();
        }
    }

}

在我的模板中,我已将该课程分配给变量$ Gmail并开始使用:

$gmailMessages = $Gmail->listMessages(GOOGLE_AUTH_API_KEY, $user_inbox["Profile_Email"]);

但我认为这不是正确的方法..我需要传递给函数什么? 感谢。

修改:我已将Google Gmail API Documentation添加到我的项目中。

修改:已解决,Google Api PHP Client

1 个答案:

答案 0 :(得分:1)

已解决,我忘了设置并调用$ client,现在我的功能是:

function listMessages($userId) {
        $client_id = API_CLIENT_ID;
        $client_secret = API_SECRET;
        $redirect_uri = API_REDIRECT_URL;
        $client = new Google_Client();
        $client->setClientId($client_id);
        $client->setClientSecret($client_secret);
        $client->setRedirectUri($redirect_uri);
        $client->addScope("https://mail.google.com/ https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile ");
        $client->setAccessType("offline");
        $service = new Google_Service_Gmail($client);
        $pageToken = NULL;
        $messages = array();
        $opt_param = array();
        do {
            try {
                if ($pageToken) {
                    $opt_param['pageToken'] = $pageToken;
                }
                $messagesResponse = $service->users_messages->listUsersMessages($userId, $opt_param);
                if ($messagesResponse->getMessages()) {
                    $messages = array_merge($messages, $messagesResponse->getMessages());
                    $pageToken = $messagesResponse->getNextPageToken();
                }
            } catch (Exception $e) {
                print 'An error occurred: ' . $e->getMessage();
            }
        } while ($pageToken);

        foreach ($messages as $message) {
            print 'Message with ID: ' . $message->getId() . '<br/>';
        }

        return $messages;
    }