Php YouTube报告API报告下载错误

Php YouTube报告API报告下载错误,php,google-api-php-client,youtube-analytics-api,Php,Google Api Php Client,Youtube Analytics Api,我代表内容所有者尝试下载YouTube Reporting API批量报告时收到错误消息 我已成功创建报表作业,报表作业返回下载URL,但下载失败,并显示以下错误消息: 发生服务错误:服务响应中的json无效: 日期、频道id、视频id、声明状态、上传者类型、国家代码、广告类型、估计youtube广告收入、广告印象、估计cpm 请有人帮我理解这个错误的含义以及如何解决这个问题?我在function downloadReport中跟踪到了这个问题。失败的线路是: response = $clien

我代表内容所有者尝试下载YouTube Reporting API批量报告时收到错误消息

我已成功创建报表作业,报表作业返回下载URL,但下载失败,并显示以下错误消息:

发生服务错误:服务响应中的json无效:

日期、频道id、视频id、声明状态、上传者类型、国家代码、广告类型、估计youtube广告收入、广告印象、估计cpm

请有人帮我理解这个错误的含义以及如何解决这个问题?我在function downloadReport中跟踪到了这个问题。失败的线路是:

response = $client->execute($request);
我在这里使用了Google开发者提供的PHP示例代码:

我的“检索报告”代码版本如下:


我怀疑与此相关,但您是否仔细检查了内容所有者id?请联系YouTube内容合作伙伴经理,确保已安装。感谢您的回复。我可以确认已传递了正确的内容所有者ID。更新:只有作为我使用的日期输出的标题行没有数据。当有数据时仍然会发生JSON错误,奇怪的是,错误消息实际上是预期的完整输出数据!我知道它是完整的,因为同一脚本的Java版本似乎工作正常,Java输出与PHP错误的比较表明数据匹配,但由于它是一个错误,所以在PHP脚本中将数据输出到文件之前,它会停止代码。这个PHP错误似乎在Google服务本身中,并且已经跟踪到API脚本“/Google/http/REST.PHP”是否还有其他人遇到过同样的错误并有修复方法?我怀疑它是否相关,但您是否仔细检查了内容所有者id?请联系YouTube内容合作伙伴经理,确保已安装。感谢您的回复。我可以确认已传递了正确的内容所有者ID。更新:只有作为我使用的日期输出的标题行没有数据。当有数据时仍然会发生JSON错误,奇怪的是,错误消息实际上是预期的完整输出数据!我知道它是完整的,因为同一脚本的Java版本似乎工作正常,Java输出与PHP错误的比较表明数据匹配,但由于它是一个错误,所以在PHP脚本中将数据输出到文件之前,它会停止代码。这个PHP错误似乎在Google服务本身中,并且已经跟踪到API脚本“/Google/http/REST.PHP”是否还有其他人遇到过同样的错误并有修复方法?
<?php

/**
 * This sample retrieves reports created by a specific job by:
 *
 * 1. Listing the jobs using the "jobs.list" method.
 * 2. Retrieving reports using the "reports.list" method.
 *
 * @author Ibrahim Ulukaya
 */

// Call set_include_path() as needed to point to your client library.
require_once '/../src/Google/autoload.php';
require_once '/../src/Google/Client.php';
require_once '/../src/Google/Service/YouTubeReporting.php';
session_start();

/*
 * You can acquire an OAuth 2.0 client ID and client secret from the
 * Google Developers Console <https://console.developers.google.com/>
 * For more information about using OAuth 2.0 to access Google APIs, please see:
 * <https://developers.google.com/youtube/v3/guides/authentication>
 * Please ensure that you have enabled the YouTube Data API for your project.
 */
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);

/*
 * This OAuth 2.0 access scope allows for full read/write access to the
 * authenticated user's account.
 */
$client->setScopes('https://www.googleapis.com/auth/yt-analytics-monetary.readonly https://www.googleapis.com/auth/yt-analytics.readonly https://www.googleapis.com/auth/youtubepartner https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtubepartner-channel-audit https://www.googleapis.com/auth/youtubepartner-content-owner-readonly');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
    FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// YouTube Reporting object used to make YouTube Reporting API requests.
$youtubeReporting = new Google_Service_YoutubeReporting($client);

if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }

  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
  try {
    if (empty(listReportingJobs($youtubeReporting, $htmlBody))) 
    {
      $htmlBody .= sprintf('<p>No jobs found.</p>');
    } 
    else
    {
        if ($_GET['reportUrl'])
        {
            downloadReport($youtubeReporting, $_GET['reportUrl'], $htmlBody);
        }
        else
        {
            if ($_GET['jobId'])
            {
               retrieveReports($youtubeReporting, $_GET['jobId'], $htmlBody);
            }
        }
    }
  } catch (Google_Service_Exception $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
  } catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
  }
  $_SESSION['token'] = $client->getAccessToken();
} else {
  // If the user hasn't authorized the app, initiate the OAuth flow
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  $htmlBody = <<<END
  <h3>Authorization Required</h3>
  <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
}


/**
 * Returns a list of reporting jobs. (jobs.listJobs)
 *
 * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.
 * @param $htmlBody - html body.
 */
function listReportingJobs(Google_Service_YouTubeReporting $youtubeReporting, &$htmlBody) {
  // Call the YouTube Reporting API's jobs.list method to retrieve reporting jobs.
  $optParams=array("onBehalfOfContentOwner"=>"REPLACE_ME");

  $reportingJobs = $youtubeReporting->jobs->listJobs($optParams);

  $htmlBody .= "<h3>Reporting Jobs</h3><ul>";
  foreach ($reportingJobs as $job) {
    $htmlBody .= sprintf('<li>id: "%s", name: "%s" report type: "%s"</li>', $job['id'],
        $job['name'], $job['reportTypeId']);
  }
  $htmlBody .= '</ul>';

  return $reportingJobs;
}

/**
 * Lists reports created by a specific job. (reports.listJobsReports)
 *
 * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.
 * @param string $jobId The ID of the job.
 * @param $htmlBody - html body.
 */
function retrieveReports(Google_Service_YouTubeReporting $youtubeReporting, $jobId, &$htmlBody) {
  // Call the YouTube Reporting API's reports.list method to retrieve reports created by a job.
  $optParams=array("onBehalfOfContentOwner"=>"REPLACE_ME");
  $reports = $youtubeReporting->jobs_reports->listJobsReports($jobId, $optParams);

  if (empty($reports)) {
    $htmlBody .= sprintf('<p>No reports found.</p>');
  } else {
    $htmlBody .= sprintf('<h2>Reports for the job "%s"</h2>', $jobId);
    foreach ($reports as $report) {
      $htmlBody .= sprintf('<ul><li>From "%s" to "%s" downloadable at "%s"</li>',
          $report['startTime'], $report['endTime'], $report['downloadUrl']);
      $htmlBody .= '</ul>';
    }
  }
}


/**
 * Download the report specified by the URL. (media.download)
 *
 * @param Google_Service_YouTubereporting $youtubeReporting YouTube Reporting service object.
 * @param string $reportUrl The URL of the report to be downloaded.
 * @param $htmlBody - html body.
 */
function downloadReport(Google_Service_YouTubeReporting $youtubeReporting, $reportUrl, &$htmlBody) {
  $client = $youtubeReporting->getClient();
  // Setting the defer flag to true tells the client to return a request which can be called
  // with ->execute(); instead of making the API call immediately.
  $client->setDefer(true);

  // Call the YouTube Reporting API's media.download method to download a report.
  $request = $youtubeReporting->media->download("");
  $request->setUrl($reportUrl);
  $response = $client->execute($request);

  file_put_contents("reportFile", $response->getResponseBody());

  $client->setDefer(false);
}
?>

<!doctype html>
<html>
<head>
<title>Retrieve reports</title>
</head>
<body>
  <form method="GET">
    <div>
      Job Id: <input type="text" id="jobId" name="jobId" placeholder="Enter Job Id">
    </div>
    <br>
    <div>
      Report URL: <input type="text" id="reportUrl" name="reportUrl" placeholder="Enter Report Url">
    </div>
    <br>    <input type="submit" value="Retrieve!">
  </form>
  <?=$htmlBody?>
</body>
</html>