gpt4 book ai didi

PHP - 带有 Google Gmail OAuth2 API 的 ListMessages

转载 作者:行者123 更新时间:2023-12-04 18:01:06 25 4
gpt4 key购买 nike

我正在创建一个基于 Web 的邮件客户端,该客户端支持使用 Google OAuth2 API 的 Google 帐户,登录工作正常,并且在首次登录时询问 Gmail 的权限,但我不明白如何从用户帐户获取邮件。根据Google Gmail API Documentation ,我正在创建一个类来使用这些 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 Api PHP Client到我的项目。

编辑:已解决,see solution here

最佳答案

解决了,我忘记设置和调用$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;
}

关于PHP - 带有 Google Gmail OAuth2 API 的 ListMessages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35292979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com