【问题标题】:Gmail API php create label as sublabelGmail API php创建标签作为子标签
【发布时间】:2021-05-25 18:19:33
【问题描述】:

我正在尝试使用方法创建一个 gmail 标签 true gmail php api 作为子标签:users.labels.create

在 Gmail api 参考中,创建一个普通标签(不是子标签)效果很好。

不幸的是,参考文献中没有讨论创建子标签,也不清楚这是否可能!

// OAuth 2.0 to access Google API

if (isset($_SESSION['access_token']) && $_SESSION['access_token']){
  require_once __DIR__.'/../../../../vendor/autoload.php';
  $client = new Google_Client();
  $client->setAuthConfig('../../../../apps/google-api-scripts/client_secrets.json');
  $client->addScope(Google_Service_Gmail::GMAIL_READONLY);
  $client->addScope(Google_Service_Gmail::GMAIL_SEND);
  $client->addScope(Google_Service_Gmail::GMAIL_MODIFY);
  $client->setAccessToken($_SESSION['access_token']);

  if ($client->getAccessToken() && $client->isAccessTokenExpired()) {
   $client->fetchAccessTokenWithRefreshToken($_COOKIE['refresh_token']);
   $_SESSION['access_token']['access_token'] = json_encode($client->getAccessToken());
   $client->setAccessToken($_SESSION['access_token']);
   }
   // IF there is no access-token found in session
   } else {
   $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/apps/google-api-scripts/oauth2callback.php';
   header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
   }

// Working example to create label:
$service = new Google_Service_Gmail($client);
$user = 'me';
$label = new Google_Service_Gmail_Label();
$label->setName($naamvoordrive);
$results = $service->users_labels->create($user, $label);
$gmaillabelid = $results['id'];

【问题讨论】:

    标签: php google-api gmail-api google-api-php-client


    【解决方案1】:

    如果要创建子标签,只需将新标签的名称设置为以下格式:

    <parent label name>/<sub-label name>

    这是一个使用 API 资源管理器的示例 users.labels.create

    请求正文示例:

    {
      "labelListVisibility": "labelShow",
      "messageListVisibility": "show",
      "name": "PARENTLABEL/CHILDLABEL"
    }
    

    输出:


    注意:

    如果您的父标签名称不存在/无效。它将根据您的集合名称创建一个带有“/”而不是子标签的标签

    示例:

    【讨论】:

      猜你喜欢
      • 2015-06-07
      • 2023-03-19
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多