Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

php - Need details of credentials setup for Analytics and Reporting API

I have created a PHP app that works fine with the Data API using OAuth. I want to adapt this to work with Analytics and Reporting API and have added the API libraries on Google cloud etc and added the PHP code to my application. When I run this I get an exception:

'The website encountered an unexpected error. Please try again later. GoogleServiceException: { "error": { "code": 403, "message": "Forbidden", "errors": [ { "message": "Forbidden", "domain": "global", "reason": "forbidden" } ] } } in GoogleHttpREST::decodeHttpResponse() (line 128 of vendor/google/apiclient/src/Http/REST.php).'

I tried the reports.query and got same result and also from the application (PHP) generated from reports.query (https://developers.google.com/youtube/analytics/reference/reports/query?...)

<?php
/**
 * Sample PHP code for youtubeAnalytics.reports.query
 * See instructions for running these code samples locally:
 * https://developers.google.com/explorer-help/guides/code_samples#php
 */

if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
  throw new Exception(sprintf('Please run "composer require google/apiclient:~2.0" in "%s"', __DIR__));
}
require_once __DIR__ . '/vendor/autoload.php';

$client = new Google_Client();
$client->setApplicationName('API code samples');
$client->setScopes([
    'https://www.googleapis.com/auth/youtube.readonly',
]);

// TODO: For this request to work, you must replace
//       "YOUR_CLIENT_SECRET_FILE.json" with a pointer to your
//       client_secret.json file. For more information, see
//       https://cloud.google.com/iam/docs/creating-managing-service-account-keys
$client->setAuthConfig('/xxx/client_secret.json');
$client->setAccessType('offline');

// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open this link in your browser:
%s
", $authUrl);
print('Enter verification code: ');
$authCode = trim(fgets(STDIN));

// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);

// Define service object for making API requests.
$service = new Google_Service_YouTubeAnalytics($client);

$queryParams = [
    'endDate' => '2021-02-01',
    'ids' => 'channel==xxxxxxxxxxxx,
    'metrics' => 'views',
    'startDate' => '2020-12-01'
];

$response = $service->reports->query($queryParams);
print_r($response);

So I think the problem is with the authorisation. Questions:

  1. Is there a step-by-step guide to create credentials etc to work with the Analytics and Reporting API (sample application)?
  2. Can the same client secret file be used for both APIs (assuming set up correctly)?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

OK so the answer seems to be in the choices made when prompted by Google to sign in. After you enter the credentials associated with the target YouTube account ([email protected]) and as set up in the credentials config, (ie not your cloud login, if different), you get a screen:

Choose your account or a brand account / to continue to Mysite.com

then 2 options:

As I wanted YT stats, and not sure what a 'brand account' was I used the second, but you should use the first. Both the 'Try this API' reports/query and my code now work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...