Google AdSense API未显示任何内容

时间:2017-07-31 10:34:24

标签: php adsense adsense-api

我正在使用php开发Google AdSense管理API,我是新手。

登录后我什么都没得到,

我正在尝试运行Example示例代码。 但登录到我的帐户后我收到了这个屏幕!

enter image description here

我的adsense-sample.php代码是:

<?php

require_once 'templates/base.php';
session_start();

/************************************************
  ATTENTION: Change this path to point to your
  client library installation!
 ************************************************/
set_include_path('google-api-php-client-2.2.0/vendor/google/apiclient-services/src' . PATH_SEPARATOR . get_include_path());

require_once 'google-api-php-client-2.2.0/vendor/autoload.php';
require_once 'Google/Service/AdExchangeBuyerII/Client.php';
require_once 'Google/Service/AdSense.php';

// Autoload example classes.
function __autoload($class_name) {
  include 'google-api-php-client-2.2.0\vendor\autoload.php';
}

// Max results per page.
define('MAX_LIST_PAGE_SIZE', 50, true);
define('MAX_REPORT_PAGE_SIZE', 50, true);

// Configure token storage on disk.
// If you want to store refresh tokens in a local disk file, set this to true.
define('STORE_ON_DISK', false, true);
define('TOKEN_FILENAME', 'tokens.dat', true);

// Set up authentication.
$client = new Google_Client();
$client->addScope('https://www.googleapis.com/auth/adsense.readonly');
$client->setAccessType('offline');

// Be sure to replace the contents of client_secrets.json with your developer
// credentials.
$client->setAuthConfigFile('client_secrets.json');

// Create service.
$service = new Google_Service_AdSense($client);

// If we're logging out we just need to clear our local access token.
// Note that this only logs you out of the session. If STORE_ON_DISK is
// enabled and you want to remove stored data, delete the file.
if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

// If we have a code back from the OAuth 2.0 flow, we need to exchange that
// with the authenticate() function. We store the resultant access token
// bundle in the session (and disk, if enabled), and redirect to this page.
if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  // Note that "getAccessToken" actually retrieves both the access and refresh
  // tokens, assuming both are available.
  $_SESSION['access_token'] = $client->getAccessToken();
  if (STORE_ON_DISK) {
    file_put_contents(TOKEN_FILENAME, $_SESSION['access_token']);
  }
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  exit;
}

// If we have an access token, we can make requests, else we generate an
// authentication URL.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else if (STORE_ON_DISK && file_exists(TOKEN_FILENAME) &&
      filesize(TOKEN_FILENAME) > 0) {
  // Note that "setAccessToken" actually sets both the access and refresh token,
  // assuming both were saved.
  $client->setAccessToken(file_get_contents(TOKEN_FILENAME));
  $_SESSION['access_token'] = $client->getAccessToken();
} else {
  // If we're doing disk storage, generate a URL that forces user approval.
  // This is the only way to guarantee we get back a refresh token.
  if (STORE_ON_DISK) {
    $client->setApprovalPrompt('force');
  }
  $authUrl = $client->createAuthUrl();
}

echo pageHeader('AdSense Management');

echo '<div><div class="request">';
if (isset($authUrl)) {
  echo '<a class="login" href="' . $authUrl . '">Connect Me!</a>';
} else {
  echo '<a class="logout" href="?logout">Logout</a>';
};
echo '</div>';

if ($client->getAccessToken()) {
  echo '<pre class="result">';
  // Now we're signed in, we can make our requests.
  makeRequests($service);
  // Note that we re-store the access_token bundle, just in case anything
  // changed during the request - the main thing that might happen here is the
  // access token itself is refreshed if the application has offline access.
  $_SESSION['access_token'] = $client->getAccessToken();
  echo '</pre>';
}

echo '</div>';
echo pageFooter(__FILE__);


// Makes all the API requests.
function makeRequests($service) {
  print "\n";
  $accounts = GetAllAccounts::run($service, MAX_LIST_PAGE_SIZE);

  if (isset($accounts) && !empty($accounts)) {
    // Get an example account ID, so we can run the following sample.
    $exampleAccountId = $accounts[0]['id'];
    GetAccountTree::run($service, $exampleAccountId);
    $adClients =
        GetAllAdClients::run($service, $exampleAccountId, MAX_LIST_PAGE_SIZE);

    if (isset($adClients) && !empty($adClients)) {
      // Get an ad client ID, so we can run the rest of the samples.
      $exampleAdClient = end($adClients);
      $exampleAdClientId = $exampleAdClient['id'];

      $adUnits = GetAllAdUnits::run($service, $exampleAccountId,
          $exampleAdClientId, MAX_LIST_PAGE_SIZE);
      if (isset($adUnits) && !empty($adUnits)) {
        // Get an example ad unit ID, so we can run the following sample.
        $exampleAdUnitId = $adUnits[0]['id'];
        GetAllCustomChannelsForAdUnit::run($service, $exampleAccountId,
          $exampleAdClientId, $exampleAdUnitId, MAX_LIST_PAGE_SIZE);
      } else {
        print 'No ad units found, unable to run dependant example.';
      }

      $customChannels = GetAllCustomChannels::run($service, $exampleAccountId,
          $exampleAdClientId, MAX_LIST_PAGE_SIZE);
      if (isset($customChannels) && !empty($customChannels)) {
        // Get an example ad unit ID, so we can run the following sample.
        $exampleCustomChannelId = $customChannels[0]['id'];
        GetAllAdUnitsForCustomChannel::run($service, $exampleAccountId,
          $exampleAdClientId, $exampleCustomChannelId, MAX_LIST_PAGE_SIZE);
      } else {
        print 'No custom channels found, unable to run dependant example.';
      }

      GetAllUrlChannels::run($service, $exampleAccountId, $exampleAdClientId,
          MAX_LIST_PAGE_SIZE);
      GenerateReport::run($service, $exampleAccountId, $exampleAdClientId);
      GenerateReportWithPaging::run($service, $exampleAccountId,
          $exampleAdClientId, MAX_REPORT_PAGE_SIZE);
      FillMissingDatesInReport::run($service, $exampleAccountId,
          $exampleAdClientId);
      CollateReportData::run($service, $exampleAccountId, $exampleAdClientId);
    } else {
      print 'No ad clients found, unable to run dependant examples.';
    }

    $savedReports = GetAllSavedReports::run($service, $exampleAccountId,
        MAX_LIST_PAGE_SIZE);
    if (isset($savedReports) && !empty($savedReports)) {
      // Get an example saved report ID, so we can run the following sample.
      $exampleSavedReportId = $savedReports[0]['id'];
      GenerateSavedReport::run($service, $exampleAccountId,
          $exampleSavedReportId);
    } else {
      print 'No saved reports found, unable to run dependant example.';
    }

    GetAllSavedAdStyles::run($service, $exampleAccountId, MAX_LIST_PAGE_SIZE);
    GetAllAlerts::run($service, $exampleAccountId);
  } else {
    'No accounts found, unable to run dependant examples.';
  }

  GetAllDimensions::run($service);
  GetAllMetrics::run($service);
}

这段代码中是否有我想要添加的内容?请帮忙。

另外,我无法确切地知道应该把哪条路径放在

  

将此路径更改为指向您的路径         客户端库安装!

在上面的代码中。(我目前推出的是正确的吗?)

0 个答案:

没有答案