0

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?

FoxFr
  • 23
  • 4
  • Please post the code creating the object `$client`. – stvar Mar 15 '21 at 13:50
  • 1
    Please edit your question and include all of your authorization code. – Linda Lawton - DaImTo Mar 15 '21 at 14:31
  • done, thanks for the tip – FoxFr Mar 15 '21 at 15:17
  • 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`](https://github.com/youtube/api-samples/blob/master/php/list_broadcasts.php). – stvar Mar 15 '21 at 15:25
  • Ok thanks, but wiuth this process i can't use without manually login ... or i don't understand something – FoxFr Mar 15 '21 at 16:02

1 Answers1

0

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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Thanks DalmTo but i have this error Type: InvalidArgumentException Message: missing the required redirect URI why ? – FoxFr Mar 15 '21 at 17:13
  • the user action is mandatory, isn't possible only with json key or other ? – FoxFr Mar 16 '21 at 14:23
  • The YouTube API doesn't support service accounts, so no there is no way around the user interaction. You need to verify it at least once. – Linda Lawton - DaImTo Mar 16 '21 at 14:25
  • Arfff ok, the user verification is needed manually ... – FoxFr Mar 16 '21 at 15:17
  • Hello Everybody, I found a solution, i explain here - logIn from my API with Google Client, and i store the access_token & refresh_token in cookie (will be secure because i'm the only one to connect my local Raspberry Pi) - the access_token stored in cookie will be use by all call to https://youtube.googleapis.com/youtube/v3/ - i use many endpoints to create brodcast, bind stream, check status of stream and stop de broadcast The refresh_token are use for an automatic refresh (offline) while the broadcast are live, to keep the status up this solution is fully ok for my usage – FoxFr Mar 21 '21 at 20:49