Google Adwords API的问题

我正在使用Google Adwords API(v201109)获取关键字及其搜索量。 我已经创建了该文件

使用以下代码的adwords.php -

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

$path = dirname(__FILE__) . '/../../../';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'Google/Api/Ads/AdWords/Lib/AdWordsUser.php';

$adwords_username = "myusername@gmail.com";
$adwords_password = "mypassword";
$adwords_developerToken = "mydevelopertoken";
$user = new AdWordsUser(null, $adwords_username, $adwords_password, $adwords_developerToken);
$user->SetDefaultServer("https://adwords.google.com/");

//set Adwords Client Id
$user->SetClientCustomerId('111111111');

$str_group = GetAdGroup($user, "v201509");
echo $str_group;

function GetAdGroup(AdWordsUser $user, $adwords_version) {
    // Get the service, which loads the required classes.
    $adgroupService = $user->GetService('AdGroupService', $adwords_version);

    // Create selector.
    $selector = new Selector();
    $selector->fields = array('Id', 'Name', 'CampaignId', 'Status');
    $selector->ordering[] = new OrderBy('CampaignId', 'ASCENDING');

    // Filter out deleted criteria.
    $selector->predicates[] = new Predicate('Status', 'NOT_IN', array('DELETED', 'PAUSED'));

    // Create paging controls.
    $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE);

    $ret = array();
    do {
    //echo "<pre>";print_r($selector);die;
        // Make the get request.
        $page = $adgroupService->get($selector);

        // Display results.
        if (isset($page->entries)) {
            foreach ($page->entries as $adgroup) {
                //printf("AdGroup with name '%s' and id '%s' was found for Campaign: '%s' and Status: '%s'n",
                // $adgroup->name, $adgroup->id, $adgroup->campaignId, $adgroup->status);
                $ret[] = array(
                    'name' => $adgroup->name,
                    'id' => $adgroup->id,
                    'campaignId' => $adgroup->campaignId,
                    'active' => (strcmp($adgroup->status,'ENABLED')==0)?1:0
                );
            }
        } else {
            //print "No adgroups were found.n";
        }
        // Advance the paging index.
        $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE;
    } while ($page->totalNumEntries > $selector->paging->startIndex);
    return $ret;
}

function GetKeywords(AdWordsUser $user, $adGroupId, $days, $adwords_version) {
    // Get the service, which loads the required classes.
    $adGroupCriterionService = $user->GetService('AdGroupCriterionService', $adwords_version);

    // Create selector.
    $selector = new Selector();

    $selector->fields = array('KeywordText', 'KeywordMatchType', 'Id', 'AverageCpc', 'AveragePosition', 'Clicks', 'Conversions', 'Cost', 'Ctr', 'Impressions', 'QualityScore', 'Status');
    $selector->ordering[] = new OrderBy('KeywordText', 'ASCENDING');

    // Create predicates.
    $selector->predicates[] = new Predicate('AdGroupId', 'IN', array($adGroupId));
    $selector->predicates[] = new Predicate('CriteriaType', 'IN', array('KEYWORD'));
    $selector->predicates[] = new Predicate('Status', 'IN', array('ACTIVE'));
    $selector->predicates[] = new Predicate('Impressions', 'GREATER_THAN', array('1'));

    $dateRange = new DateRange();
    $str1 = '-1 days';
    $str2 = '-1 days';
    if ($days > 0) {
    $str1 = '-' . $days . ' days';
    $str2 = '-' . $days . ' days';
    }
    $dateRange->min = date('Ymd', strtotime($str1));
    $dateRange->max = date('Ymd', strtotime($str2));
    $selector->dateRange = $dateRange;

    // Create paging controls.
    $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE);

    $ret = array();

    do {
    // Make the get request.
    $page = $adGroupCriterionService->get($selector);

    // Display results.
    if (isset($page->entries)) {
        foreach ($page->entries as $adGroupCriterion) {
        $cr = 0;
        if ($adGroupCriterion->stats->clicks > 0)
            $cr = $adGroupCriterion->stats->conversions / $adGroupCriterion->stats->clicks;

        $ret[] = array(
            'name' => $adGroupCriterion->criterion->text,
            'type' => $adGroupCriterion->criterion->matchType,
            'id' => $adGroupCriterion->criterion->id,
            'clicks' => $adGroupCriterion->stats->clicks,
            'cpc' => $adGroupCriterion->stats->averageCpc->microAmount / 1000000,
            'conversions' => $adGroupCriterion->stats->conversions,
            'cost' => $adGroupCriterion->stats->cost->microAmount / 1000000,
            'ctr' => $adGroupCriterion->stats->ctr,
            'impressions' => $adGroupCriterion->stats->impressions,
            'qualityfactor' => $adGroupCriterion->qualityInfo->qualityScore,
            'cr' => $cr,
            'position' => $adGroupCriterion->stats->averagePosition
        );
        }
    } else {
        //print "No keywords were found.n";
    }

    // Advance the paging index.
    $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE;
    } while ($page->totalNumEntries > $selector->paging->startIndex);
    return $ret;
}

但是当我运行adwords.php文件时,我不断收到以下错误消息。

错误

致命错误:未捕获SoapFault异常:[soap:Server] [QuotaCheckError.INVALID_TOKEN_HEADER @; 触发器:'abhijeetk.aloha@gmail.com']位于/var/www/html/Google/Api/Ads/Common/Lib/AdsSoapClient.php:216堆栈跟踪:#0 / var / www / html / Google / Api /Ads/Common/Lib/AdsSoapClient.php(216):SoapClient - > __ soapCall('get',Array,NULL,Array,Array)#1 / var / www / html / Google / Api / Ads / AdWords / v201509 / AdGroupService.php(7408):AdsSoapClient - > __ soapCall('get',Array)#2 /var/www/html/adwords.php(46):AdGroupService-> get(Object(Selector))

3 /var/www/html/adwords.php(22):GetAdGroup(Object(AdWordsUser),'v201509')#4 {main}抛出

第216行/var/www/html/Google/Api/Ads/Common/Lib/AdsSoapClient.php

我是新手,所以我不知道如何使用这些API来获取数据,请告诉我我做错了什么。 或者,如何使用AdWords API来获取关键字及其卷和其他数据。 我发现的所有文档都让我更加困惑。

提前致谢


__construct($authenticationIniPath = null,
      $developerToken = null, $userAgent = null, $clientCustomerId = null,
      $settingsIniPath = null, $oauth2Info = null)

$user = new AdWordsUser(null, $adwords_username, $adwords_password, $adwords_developerToken);

错误的数据传递; 改用auth.ini。 https://developers.google.com/adwords/api/docs/first-request


您收到此错误是因为您尚未设置开发人员令牌或您的开发人员令牌仍处于待批准模式下。

您可以将开发人员令牌设置为以下文件: libGoogleApiAdsAdWordsauth.ini

链接地址: http://www.djcxy.com/p/32353.html

上一篇: Problems with Google Adwords API

下一篇: One Adwords API Developer Token for multiple accounts?