Aktywowanie i dezaktywowanie segmentów danych

W tym przewodniku opisujemy metody, których możesz użyć do aktywowania lub dezaktywowania jednego ze swoich segmentów danych.

Zanim zaczniesz

Zanim przejdziesz dalej, musisz skonfigurować uwierzytelnianie.

Aktywowanie segmentu danych

Aby aktywować jeden ze swoich segmentów danych, użyj curators.dataSegments.activate metody. Kupujący mogą kierować reklamy na segmenty danych w ramach transakcji lub pakietów.

Poniższy przykład wysyła żądanie POST do metody curators.dataSegments.activate, która aktywuje określony segment danych.

REST

Żądanie

curl --request POST \
'https://authorizedbuyersmarketplace.googleapis.com/v1beta/curators/ACCOUNT_ID/dataSegments/test-data-segment-1:activate' \
--header 'Authorization: Bearer ACCESS_TOKEN' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{}' \
--compressed

Zastąp te elementy:

  • ACCOUNT_ID: identyfikator Twojego konta.
  • ACCESS_TOKEN: Twój token dostępu.

Odpowiedź

{
  "name": "curators/ACCOUNT_ID/dataSegments/test-data-segment-1",
  "createTime": "2025-08-13T12:14:03.421Z",
  "updateTime": "2025-08-14T19:47:40.253938Z",
  "cpmFee": {
    "currencyCode": "USD",
    "units": "1000000"
  },
  "state": "ACTIVE"
}

Java

/*
 * Copyright (c) 2026 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express
 * or implied. See the License for the specific language governing permissions and limitations
under
 * the License.
 */

package com.google.api.services.samples.authorizedbuyers.marketplace.v1beta.curators.dataSegments;

import com.google.api.services.authorizedbuyersmarketplace.v1beta.AuthorizedBuyersMarketplace;
import com.google.api.services.authorizedbuyersmarketplace.v1beta.model.ActivateDataSegmentRequest;
import com.google.api.services.authorizedbuyersmarketplace.v1beta.model.DataSegment;
import com.google.api.services.samples.authorizedbuyers.marketplace.v1beta.Utils;
import java.io.IOException;
import java.security.GeneralSecurityException;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;

public class ActivateDataSegment {

  private ActivateDataSegment() {}

  /**
   * Executes the activate operation for a data segment.
   *
   * @param marketplaceClient the initialized Marketplace API client.
   * @param accountId the account ID of the curator account.
   * @param dataSegmentId the resource ID of the data segment to activate.
   * @throws IOException if the API returns an error.
   */
  public static void execute(
      AuthorizedBuyersMarketplace marketplaceClient, Long accountId, String dataSegmentId)
      throws IOException {

    String name = String.format("curators/%d/dataSegments/%s", accountId, dataSegmentId);

    System.out.printf("Activating data segment with name \"%s\".%n", name);

    DataSegment response =
        marketplaceClient
            .curators()
            .dataSegments()
            .activate(name, new ActivateDataSegmentRequest())
            .execute();

    System.out.println("Successfully activated data segment:");
    Utils.jsonPrettyPrint(response);
  }

  /**
   * Creates and configures the ArgumentParser for this sample.
   *
   * @return the configured ArgumentParser.
   */
  private static ArgumentParser createArgumentParser() {
    ArgumentParser parser =
        ArgumentParsers.newFor("ActivateDataSegment")
            .build()
            .defaultHelp(true)
            .description("Activates a specific data segment.");

    // Required arguments.
    parser
        .addArgument("-a", "--account_id")
        .help("The account ID of the curator that created the data segment.")
        .required(true)
        .type(Long.class);
    parser
        .addArgument("-d", "--data_segment_id")
        .help("The resource ID of the data segment to activate.")
        .required(true);

    return parser;
  }

  public static void main(String[] args) {
    ArgumentParser parser = createArgumentParser();
    Namespace parsedArgs = null;
    try {
      parsedArgs = parser.parseArgs(args);
    } catch (ArgumentParserException e) {
      parser.handleError(e);
      System.exit(1);
    }

    AuthorizedBuyersMarketplace client = null;
    try {
      client = Utils.getMarketplaceClient();
    } catch (IOException ex) {
      System.out.printf("Unable to create Marketplace API service:%n%s", ex);
      System.out.println("Did you specify a valid path to a service account key file?");
      System.exit(1);
    } catch (GeneralSecurityException ex) {
      System.out.printf("Unable to establish secure HttpTransport:%n%s", ex);
      System.exit(1);
    }

    Long accountId = parsedArgs.getLong("account_id");
    String dataSegmentId = parsedArgs.getString("data_segment_id");

    try {
      execute(client, accountId, dataSegmentId);
    } catch (Exception e) {
      System.out.printf("Marketplace API returned error response:%n%s", e);
      System.exit(1);
    }
  }
}

Dezaktywowanie segmentu danych

Aby dezaktywować jeden ze swoich segmentów danych, użyj metody curators.dataSegments.deactivate.

Poniższy przykład wysyła żądanie POST do metody curators.dataSegments.deactivate, która dezaktywuje określony segment danych.

REST

Żądanie

curl --request POST \
'https://authorizedbuyersmarketplace.googleapis.com/v1beta/curators/ACCOUNT_ID/dataSegments/test-data-segment-1:deactivate' \
--header 'Authorization: Bearer ACCESS_TOKEN' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{}' \
--compressed

Odpowiedź

{
  "name": "curators/ACCOUNT_ID/dataSegments/test-data-segment-1",
  "createTime": "2025-08-13T12:14:03.421Z",
  "updateTime": "2025-08-14T20:05:51.439110Z",
  "cpmFee": {
    "currencyCode": "USD",
    "units": "1000000"
  },
  "state": "INACTIVE"
}

Java

/*
 * Copyright (c) 2026 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express
 * or implied. See the License for the specific language governing permissions and limitations
under
 * the License.
 */

package com.google.api.services.samples.authorizedbuyers.marketplace.v1beta.curators.dataSegments;

import com.google.api.services.authorizedbuyersmarketplace.v1beta.AuthorizedBuyersMarketplace;
import com.google.api.services.authorizedbuyersmarketplace.v1beta.model.DataSegment;
import com.google.api.services.authorizedbuyersmarketplace.v1beta.model.DeactivateDataSegmentRequest;
import com.google.api.services.samples.authorizedbuyers.marketplace.v1beta.Utils;
import java.io.IOException;
import java.security.GeneralSecurityException;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;

public class DeactivateDataSegment {

  private DeactivateDataSegment() {}

  /**
   * Executes the deactivate operation for a data segment.
   *
   * @param marketplaceClient the initialized Marketplace API client.
   * @param accountId the account ID of the curator account.
   * @param dataSegmentId the resource ID of the data segment to deactivate.
   * @throws IOException if the API returns an error.
   */
  public static void execute(
      AuthorizedBuyersMarketplace marketplaceClient, Long accountId, String dataSegmentId)
      throws IOException {

    String name = String.format("curators/%d/dataSegments/%s", accountId, dataSegmentId);

    System.out.printf("Deactivating data segment with name \"%s\".%n", name);

    DataSegment response =
        marketplaceClient
            .curators()
            .dataSegments()
            .deactivate(name, new DeactivateDataSegmentRequest())
            .execute();

    System.out.println("Successfully deactivated data segment:");
    Utils.jsonPrettyPrint(response);
  }

  /**
   * Creates and configures the ArgumentParser for this sample.
   *
   * @return the configured ArgumentParser.
   */
  private static ArgumentParser createArgumentParser() {
    ArgumentParser parser =
        ArgumentParsers.newFor("DeactivateDataSegment")
            .build()
            .defaultHelp(true)
            .description("Deactivates a specific data segment.");

    // Required arguments.
    parser
        .addArgument("-a", "--account_id")
        .help("The account ID of the curator that created the data segment.")
        .required(true)
        .type(Long.class);
    parser
        .addArgument("-d", "--data_segment_id")
        .help("The resource ID of the data segment to deactivate.")
        .required(true);

    return parser;
  }

  public static void main(String[] args) {
    ArgumentParser parser = createArgumentParser();
    Namespace parsedArgs = null;
    try {
      parsedArgs = parser.parseArgs(args);
    } catch (ArgumentParserException e) {
      parser.handleError(e);
      System.exit(1);
    }

    AuthorizedBuyersMarketplace client = null;
    try {
      client = Utils.getMarketplaceClient();
    } catch (IOException ex) {
      System.out.printf("Unable to create Marketplace API service:%n%s", ex);
      System.out.println("Did you specify a valid path to a service account key file?");
      System.exit(1);
    } catch (GeneralSecurityException ex) {
      System.out.printf("Unable to establish secure HttpTransport:%n%s", ex);
      System.exit(1);
    }

    Long accountId = parsedArgs.getLong("account_id");
    String dataSegmentId = parsedArgs.getString("data_segment_id");

    try {
      execute(client, accountId, dataSegmentId);
    } catch (Exception e) {
      System.out.printf("Marketplace API returned error response:%n%s", e);
      System.exit(1);
    }
  }
}

Dalsze kroki