2017-07-23 25 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

很高兴知道。 –

相关问题