简体   繁体   English

无法删除GMAIL PHP API中的标签

[英]Unable to remove label in GMAIL PHP API

I am working on a project where I have several labels associated with every email. 我正在开展一个项目,我在每个电子邮件中都有几个标签。 I want to remove the labels using gmail PHP API. 我想使用gmail PHP API删除标签。 I have followed the documentation and I have done all the steps. 我已经按照文档进行了操作,并完成了所有步骤。 But, I do not know why I get Error, when I try to remove the label. 但是,当我尝试删除标签时,我不知道为什么会出现错误。

This is the code that is associated with the project. 这是与项目关联的代码。 Please help me with any thoughts. 请帮助我任何想法。

  $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();
   }
 }

As given in Standard Error Responses for Google APIs, 403: insufficientPermissions error code means that the authenticated user does not have sufficient permissions to execute this request. 如Google API的标准错误响应中所述, 403: insufficientPermissions错误代码表示经过身份验证的用户没有足够的权限来执行此请求。

To delete labels, you should have this scope code in your permissions: 要删除标签,您应该在权限中包含此范围代码:

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

For more details about scopes, please go through Choose Auth Scopes . 有关范围的更多详细信息,请通过选择身份验证范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM