0
votes

I try to use the YouTube API, but when I wish to use the LiveBroadcasts.list API. When I use the same JSON key to list my playlist it's OK... I don't understand why.

$client = new \Google_Client();
        $client->setAuthConfig(__DIR__ . '/../../key/youtube_client.json');
        $client->setApplicationName('Broadcast');
        $client->setScopes([
            'https://www.googleapis.com/auth/youtube',
            'https://www.googleapis.com/auth/youtube.force-ssl',
            'https://www.googleapis.com/auth/youtube.upload',
            'https://www.googleapis.com/auth/youtube.readonly'
        ]);

$service = new \Google_Service_YouTube($client);
    
$broadcastsResponse = $service->liveBroadcasts->listLiveBroadcasts(
    'id,snippet,contentDetails',
    array(
        'broadcastType' => 'persistent',
        'mine' => 'true',
    )
);

The error message is:

{ "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Login Required.", "domain": "global", "reason": "required", "location": "Authorization", "locationType": "header" } ], "status": "UNAUTHENTICATED" } }

Can someone help me to know where is my mistake?

1
Please post the code creating the object $client.stvar
Please edit your question and include all of your authorization code.DaImTo
done, thanks for the tipFoxFr
Please follow @DaImTo's request to post all of your authorization code. The part you've shown is OK, but you've not posted the code that runs the OAuth 2 authentication/authorization flow. See this public Google sample code: list_broadcasts.php.stvar
Ok thanks, but wiuth this process i can't use without manually login ... or i don't understand somethingFoxFr

1 Answers

0
votes

You appear to be missing a lot of the authorization code that is required in order to fetch an access token.

<?php

/**
 * Sample PHP code for youtube.liveBroadcasts.list
 * 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('YOUR_CLIENT_SECRET_FILE.json');
$client->setAccessType('offline');

// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open this link in your browser:\n%s\n", $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_YouTube($client);

$response = $service->liveBroadcasts->listLiveBroadcasts('');
print_r($response);

This sample is going to end up running more as a console application as it just builds the url for you. If you need it run as a web application let me know i have some other sampes just not for this API which you can find here. If you need help altering it let me know.