3
编辑:修正了第一个错误!现在列出邮件ID,仍然试图解决如何从收件箱中显示完整电子邮件,如果任何人都可以提供帮助,将不胜感激。GMail API - 如何显示收件箱中的完整电子邮件
所以我试图使用GMail API来调用我的收件箱中的电子邮件,所以我可以在我自己的应用程序中使用它们。我已经得到所有访问排序,并且目前有一个数组正在返回。我遇到的问题是试图显示消息。
我的PHP看起来如下
<?php
ini_set('display_errors' , 'On');
error_reporting(E_ALL);
// Start Session
session_start();
//Include API autoloader
require_once __DIR__ . '/vendor/autoload.php';
// Create Google Client
$client = new Google_Client();
$client->setClientId('ID in here');
$client->setClientSecret('Secret here');
$client->setRedirectUri('http://localhost:8888/gmail/');
$client->addScope('https://mail.google.com/');
//Create Google Service
$service = new Google_Service_Gmail($client);
if(isset($_REQUEST['logout'])){
unset($_SESSION['access_token']);
}
// Check if there is an auth token
if(isset($_GET['code']))
{
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($url, FILTER_VALIDATE_URL));
}
// Check for access token in session
if(isset($_SESSION['access_token']))
{
$client->setAccessToken($_SESSION['access_token']);
} else
{
$loginUrl = $client->createAuthUrl();
echo 'Click <a href="' . $loginUrl . '">here</a> to login';
}
// Check if we have an access token ready for API Call
try
{
if(isset($_SESSION['access_token']) && $client->getAccessToken())
{
//make API calls
$messages = $service->users_messages->listUsersMessages('me',['maxResults'=> 2, 'labelIds'=> 'INBOX']);
foreach ($messages as $message)
{
print 'Message with ID: ' . $message->getId() . '<br/>';
}
return $messages;
}
}
catch (Google_Auth_Exception $e)
{
echo 'Looks like you dont have an access token or its expired';
}
您必须按照以前的方式列出消息,但必须向[** get **]发出单独的请求( https://developers.google.com/gmail/api/v1/reference/users/messages/get#try-it)邮件内容。 [**这个答案**](http://stackoverflow.com/questions/32655874/cannot-get-the-body-of-email-with-gmail-php-api/32660892#32660892)可能会有所帮助。 – Tholle