2016-02-09 57 views
2

我創建一個基於Web的郵件客戶端,支持谷歌帳戶使用谷歌OAuth2 API,登錄工作正常和第一次登錄時問Gmail的權限,但我不明白如何獲得來自用戶帳戶的郵件。按照Google Gmail API Documentation,我創建一個類來使用這些API,但我不知道如何使用它..PHP - ListMessages與谷歌Gmail OAuth2 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

回答

1

解決了,我忘了設置和調用$客戶端,現在我的作用是:

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; 
    }