编程语言
首页 > 编程语言> > php – Gmail api.如何获取消息标签

php – Gmail api.如何获取消息标签

作者:互联网

如何从Google_Service_Gmail_Message或messageId获取消息labelIds

类似的东西:

$messages = $gmailService()->users_messages->listUsersMessages('me', ['q' => 'newer_than:1d in:anywhere']);

foreach ($messages as $message) {
    $messageLabels = $message->getLabelIds();
}

解决方法:

如果你试试页面底部的API Explorer,你会看到列出的消息只给你消息的ID:

响应

{
 "messages": [
  {
   "id": "1527ddcca0fd0e08",
   "threadId": "1525a22606f6d608"
  },
  {
   "id": "1527d0e3b13fab83",
   "threadId": "152792b4f30977ae"
  }, ...
 ],
 "nextPageToken": "13090329777308767238",
 "resultSizeEstimate": 100
}

您需要使用消息ID单独获取这些消息,您可以使用try here

响应

{
 "id": "1527ddcca0fd0e08",
 "threadId": "1525a22606f6d608",
 "labelIds": [
  "INBOX",
  "IMPORTANT",
  "CATEGORY_FORUMS"
 ],
 "historyId": "721186",
 "internalDate": "1453810567000",
 "payload": {
  "mimeType": "multipart/alternative",
  "filename": "", ...

如您所见,此响应包含labelIds.

示例(刚收到第一条消息)

$messages = $service->users_messages->listUsersMessages('me', ['q' => 'newer_than:1d in:anywhere']);
$list = $messages->getMessages();
$messageId = $list[0]->getId();
$message = $service->users_messages->get('me', $messageId, ['format' => 'full']);
$labelIds = $message->getLabelIds(); 

标签:php,gmail-api
来源: https://codeday.me/bug/20190519/1135818.html