【问题标题】:Unable to remove label in GMAIL PHP API无法删除 GMAIL PHP API 中的标签
【发布时间】:2017-01-06 02:14:54
【问题描述】:

我正在开展一个项目,在该项目中,每封电子邮件都有多个标签。我想使用 gmail PHP API 删除标签。我已按照文档进行操作,并且已完成所有步骤。但是,当我尝试删除标签时,我不知道为什么会出现错误。

这是与项目关联的代码。请帮我解决任何想法。

  $client_id = 'aoppedisano@tecnavi.com';
  $service_account_name = 'anthony@teak-truck- 130612.iam.gserviceaccount.com';
  $key_file_location = 'anthony.p12';
  $userid_from='aoppedisano@tecnavi.com';
  $client = new Google_Client();
  var_dump($client);
  $client->setScopes(array('https://www.googleapis.com/auth/gmail.modify'));
  $client->setApplicationName("Client_Library_Examples");
  if (isset($_SESSION['service_token'])) {
    $client->setAccessToken($_SESSION['service_token']);
 }
 $key = file_get_contents($key_file_location);
 $cred = new Google_Auth_AssertionCredentials(
  $service_account_name,
  array(
 /*     
    'https://www.googleapis.com/auth/gmail.send',
    'https://www.googleapis.com/auth/gmail.compose',
    'https://www.googleapis.com/auth/gmail.modify',
*/
    'https://www.googleapis.com/auth/gmail.readonly'
 ),
   $key
   );
   //var_dump($cred);
  $cred->sub=$userid_from; //<-- Important!
  $client->setAssertionCredentials($cred);

  if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
 }
 $service = new Google_Service_Gmail($client);
 $messageId=$_REQUEST["id"];
 $userId = 'me';
 $optParamsGet = [];
 $optParamsGet['format'] = 'full';
 $message = $service->users_messages->get('me',$messageId,$optParamsGet);
 $labelsToRemove=$_REQUEST['label'];
 $labelsToAdd=[];
 $message=modifyMessage($service,$userId, $messageId, $labelsToAdd,         $labelsToRemove);



    function modifyMessage($service, $userId, $messageId, $labelsToAdd,  $labelsToRemove) {
     $mods = new Google_Service_Gmail_ModifyMessageRequest();
     $mods->setAddLabelIds($labelsToAdd);
     $mods->setRemoveLabelIds($labelsToRemove);
    try {
     $message = $service->users_messages->modify($userId, $messageId, $mods);
     print 'Message with ID: ' . $messageId . ' successfully modified.';
     return $message;
    } catch (Exception $e) {
     print 'An error occurred: ' . $e->getMessage();
   }
 }

【问题讨论】:

标签: php gmail gmail-api gmail-imap gmail-pop


【解决方案1】:

正如Standard Error Responses 中针对 Google API 给出的,403: insufficientPermissions 错误代码意味着经过身份验证的用户没有足够的权限来执行此请求。

要删除标签,您的权限中应包含此范围代码:

https://www.googleapis.com/auth/gmail.labels

有关范围的更多详细信息,请通过Choose Auth Scopes

【讨论】:

  • 您好 Teyam,感谢您回答我的问题。这两种方法我都试过了,如有错误请指正。
  • setScopes(array('googleapis.com/auth/gmail.labels')); $client->setApplicationName("Client_Library_Examples");
  • if (isset($_SESSION['service_token'])) { $client->setAccessToken($_SESSION['service_token']); } //var_dump($client->getAccessToken()); $key = file_get_contents($key_file_location); //var_dump($key); $cred = new Google_Auth_AssertionCredentials( $service_account_name, array( /* 'googleapis.com/auth/gmail.send', 'googleapis.com/auth/gmail.compose', 'googleapis.com/auth/gmail.modify', */ 'googleapis.com/auth/gmail.labels' ),
  • 但是他们都给了我错误。第一个给了我关于 tokenresfresh 的错误,第二个给了我与我发布的错误相同的错误。请帮我。谢谢。
  • 您最初能否通过 Gmail 授权您的应用?请注意,对 Gmail API 的所有请求都必须由经过身份验证的用户授权。您可以参考Authorizing Your App with Gmail 了解更多信息并帮助您开始授权。
猜你喜欢
  • 2017-04-23
  • 2017-12-04
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 2018-07-22
  • 2021-10-13
相关资源
最近更新 更多