5

有谁知道如何使用 Google API 从我们的 Google Adwords 帐户中阻止某些 IP 地址?

本文中,您可以看到如何手动执行此操作,但我找不到以编程方式执行此操作的方法。

4

2 回答 2

5

我知道已经晚了。但我也需要它。
我在这里IpBlock找到了Google AdWords 上的类型。

于 2015-07-28T15:52:20.993 回答
2

这是我找到的一些示例代码,并稍微改进了 require_once 此代码,然后为您想要禁止 IP 的每个活动进行此调用

YourNameSpace\BlockedIP::add($campaignId, $ip);

<?php
/*
    ...
*/

namespace YourNameSpace;

use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Google\AdsApi\AdWords\v201802\cm\CampaignCriterionService;
use Google\AdsApi\AdWords\v201802\cm\IpBlock;
use Google\AdsApi\AdWords\v201802\cm\NegativeCampaignCriterion;
use Google\AdsApi\AdWords\v201802\cm\CampaignCriterionOperation;
use Google\AdsApi\AdWords\v201802\cm\Operator;

class BlockedIP {
  public static function runExample(AdWordsServices $adWordsServices,
        AdWordsSession $session,
        $campaignId,
        $ip) {

    $campaignCriterionService =
        $adWordsServices->get($session, CampaignCriterionService::class);


    $campaignCriteria = [];

    // Add a negative campaign criterion.
    $ipBlock = new IpBlock();
    $ipBlock->setIpAddress($ip);
    $negativeCriterion = new NegativeCampaignCriterion();
    $negativeCriterion->setCampaignId($campaignId);
    $negativeCriterion->setCriterion($ipBlock);

    $operation = new CampaignCriterionOperation();
    $operation->setOperator(Operator::ADD);
    $operation->setOperand($negativeCriterion);
    $operations[] = $operation;

    $result = $campaignCriterionService->mutate($operations);

    // Print out some information about added campaign criteria.
    foreach ($result->getValue() as $campaignCriterion) {
      printf(
          "Campaign targeting criterion with ID %d and type '%s' was added.\n",
          $campaignCriterion->getCriterion()->getId(),
          $campaignCriterion->getCriterion()->getType());
    }
  }

  public static function add($campaignId, $ip) {
    // Generate a refreshable OAuth2 credential for authentication.
    $oAuth2Credential = (new OAuth2TokenBuilder())
        ->fromFile()
        ->build();

    // Construct an API session configured from a properties file and the OAuth2
    // credentials above.
    $session = (new AdWordsSessionBuilder())
        ->fromFile()
        ->withOAuth2Credential($oAuth2Credential)
        ->build();

    self::runExample(new AdWordsServices(), $session, $campaignId, $ip);
  }
}
于 2018-08-20T22:45:42.730 回答