2017-07-23 29 views
1

我試圖使用php庫獲取郵件列表。這是工作,但現在它會顯示以下錯誤:Gmail API獲取列表「發生了錯誤:{」error「:」invalid_grant「,」error_description「:」Bad Request「}」PHP

發生錯誤:{ 「錯誤」: 「invalid_grant」, 「ERROR_DESCRIPTION」: 「錯誤的請求」}

陣列(0) {}

代碼:

function getList($lastsynctime='') {  
    // Get the API client and construct the service object. 
    $client = $this->getClient(); 
    $service = new Google_Service_Gmail($client); 

    // $newTime = strtotime('-15 minutes'); 
    if ($lastsynctime =='') {   
    $newTime = strtotime('-60 day'); 
    $after = strtotime(date('Y-m-d H:i:s', $newTime)); 
    }else{ 
    $after = strtotime($lastsynctime); 
    } 

    // Print the labels in the user's account. 
    $userId = 'me'; 
    $pageToken = NULL; 
    $messages = array(); 
    $opt_param = array(); 
    do { 
    try { 
     if ($pageToken) { 
     $opt_param['pageToken'] = $pageToken; 
     } 

     $opt_param['q'] = "from:[email protected] after:$after"; 

     $messagesResponse = $service->users_messages->listUsersMessages($userId, $opt_param); 
    echo "<pre>";var_dump($messagesResponse); 

    } catch (Exception $e) { 
     print 'An error occurred: ' . $e->getMessage(); 
    } 
    } while ($pageToken); 


    return $messagesResponse; 
} 

回答

1

嘛!由於你的代碼在以前工作,你沒有改變任何東西。那麼我認爲這個問題不在你的代碼中。

您可以檢查關聯的應用和網站設置您的Gmail 我的帳戶菜單下,以確保您的應用程序是存在的。

如果應用程序在那裏,但還沒有工作,那麼你需要刪除應用程序,並重新生成你的客戶端密鑰。因爲您的客戶機密可能會過期。

+0

感謝您的建議。它解決了我的問題。 –

+0

很高興知道。 –

相關問題