Gmail API获取列表“;出现错误:{quot;error";:“无效的”授权“,“error”描述“:“错误的请求”}”;在PHP中

Gmail API获取列表“;出现错误:{quot;error";:“无效的”授权“,“error”描述“:“错误的请求”}”;在PHP中,api,gmail,Api,Gmail,我正在尝试使用php库获取邮件列表。它以前工作,但现在显示以下错误: 发生错误:{“错误”:“无效的授权”,“错误描述”: “错误请求”} 数组(0){} 代码: 函数getList($lastsynctime=''){ //获取API客户端并构造服务对象。 $client=$this->getClient(); $service=新的Google\u服务\u Gmail($client); //$newTime=strottime('-15分钟'); 如果($lastsynctime=''){

我正在尝试使用php库获取邮件列表。它以前工作,但现在显示以下错误:

发生错误:{“错误”:“无效的授权”,“错误描述”: “错误请求”}

数组(0){}

代码:

函数getList($lastsynctime=''){
//获取API客户端并构造服务对象。
$client=$this->getClient();
$service=新的Google\u服务\u Gmail($client);
//$newTime=strottime('-15分钟');
如果($lastsynctime=''){
$newTime=strottime(“-60天”);
$after=strotime(日期('Y-m-d H:i:s',$newTime));
}否则{
$after=strottime($lastsynctime);
}
//打印用户帐户中的标签。
$userId='me';
$pageToken=NULL;
$messages=array();
$opt_param=array();
做{
试一试{
如果($pageToken){
$opt_param['pageToken']=$pageToken;
}
$opt_param['q']=”来自:example@gmail.com之后:$之后”;
$messagesResponse=$service->users\u messages->listUsersMessages($userId,$opt\u param);
echo”“;var_dump($messagesResponse);
}捕获(例外$e){
打印“出现错误:”。$e->getMessage();
}
}而($pageToken);
返回$messagesResponse;
}

好吧!因为您的代码以前是有效的,您没有做任何更改。那么我认为问题不在于你的代码

你可以在你的gmail我的账户菜单下检查你的连接的应用程序和网站设置,以确保你的应用程序在那里


如果应用程序已存在但尚未运行,则需要删除该应用程序并重新生成客户端密钥。因为你的客户机密可能会过期。

好吧!因为您的代码以前是有效的,您没有做任何更改。那么我认为问题不在于你的代码

你可以在你的gmail我的账户菜单下检查你的连接的应用程序和网站设置,以确保你的应用程序在那里


如果应用程序已存在但尚未运行,则需要删除该应用程序并重新生成客户端密钥。因为您的客户端密码可能会过期。

我在不同的API(谷歌搜索控制台)中发现了此错误

尝试从你的Gmail帐户访问


查看具有帐户访问权限的应用程序下是否显示
驾驶测试密钥
。如果没有,那么正如@Mahbubul Islam提到的,您需要再次创建凭据。

我在不同的API(谷歌搜索控制台)中遇到了这个错误

尝试从你的Gmail帐户访问


查看具有帐户访问权限的应用程序下是否显示
驾驶测试密钥
。如果没有,那么正如@Mahbubul Islam提到的,您需要再次创建凭据。

很高兴知道。很高兴知道。
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:example@gmail.com 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;
}