如何在没有oauth的情况下通过Gmail(谷歌)API阅读我的Gmail收件箱邮件

时间:2017-08-11 07:34:02

标签: php email oauth

我已经构建了一个代码,通过该代码我可以阅读我的收件箱消息,但每次我再次打开浏览器时都需要对应用程序进行身份验证,实际上,我只想阅读一个电子邮件地址的电子邮件(我的电子邮件地址) )。所以我认为必须有另一种方法来访问我的Gmail INBOX,没有身份验证过程。顺便说一下这是我的代码

   <?php
       session_start();
     require_once 'google-api-php-client/vendor/autoload.php';
     $client = new Google_Client;
     $client->setClientId('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
     $client->setClientSecret('xxxxxxxxxxxxxxxxxxxx');
     $client->setRedirectUri('http://localhost/gmail_api/redirect.php');
     $client->addScope(Google_Service_Gmail::GMAIL_READONLY);
     $client->setAccessType('offline');    

    if(isset($_GET['code'])){
   $client->authenticate($_GET['code']);
   $access_token = $client->getAccessToken();
   $_SESSION['access_token']= $access_token; 
   header('Location:redirect.php');
   }

 ?>

   <!doctype html>
  <html>
  <body>
<?php  if(!isset($_SESSION['access_token'])): ?>

 <a href="<?php echo $client->createAuthUrl(); ?>"> LogIn to google </a>
 <?php else:
 $access_token = $_SESSION['access_token'];
 $client->setAccessToken($access_token);
   ?>
You are Signin
<a href = "logout.php">logout </a>
<form method="get" action="">
<input type="text" name="search">
<input type="submit" value="search">
</form>
<?php endif; ?>
    <?php
            $service = new Google_Service_Gmail($client);
            function getHeader($headers, $name) {
   foreach($headers as $header) {
if($header['name'] == $name) {
  return $header['value'];
   }
 }
}
 try{      $optParams = [];
            $optParams['maxResults'] = 500; // Return Only 5 Messages
            $optParams['labelIds'] = 'INBOX'; // Only show messages in Inbox
            $optParams['includeSpamTrash'] = false;
            $optParams['q'] = $_GET['search'];
            $messages = $service->users_messages->listUsersMessages('me',$optParams);

            $list = $messages->getMessages();

            foreach($list as $value){
            $messageId = $value->getId(); // Grab first Message
             $optParamsGet = [];
            $optParamsGet['format'] = 'full'; // Display message in payload
            $message = $service->users_messages->get('me',$messageId,$optParamsGet);
            $messagePayload = $message->getPayload();
          $From = getHeader($headers,'From');
            echo "<pre>";
            print_r($messagePayload);
            echo "</pre>";
            $headers = $message->getPayload()->getHeaders();
            $parts = $message->getPayload()->getParts();
            $subject = getHeader($headers,'Subject');
            $Date = getHeader($headers,'Date');
            $From = getHeader($headers,'From');
            echo "<pre>";
            print_r($parts);
            echo "</pre>";
            $text_data = $parts[0]['body']->data;
            $body = $parts[1]['body'];
            $rawData = $body->data;
            $sanitizedData = strtr($rawData,'-_', '+/');
            $decodedMessage = base64_decode($sanitizedData);
             $text_data = base64_decode(strtr($text_data,'-_', '+/'));
    echo "<h2>$subject </h2>";
    echo "Date:$Date <br>";
    echo "From:". htmlspecialchars($From)."<br>";
    print_r($text_data);
    print_r($decodedMessage);
}

        } catch (Exception $e) {
            print($e->getMessage());
            unset($_SESSION['access_token']);
        }
  ?>

 </body>
 </html> 

0 个答案:

没有答案