Youtube Data API UnknowPart Error When Create Broadcasting

Tags: , , ,



I’m trying to create Youtube live stream through my webpage via Youtube Data API. Whatever I tried, keep getting that error:

{
  "error": {
    "code": 400,
    "message": "'{0}'",
    "errors": [
      {
        "message": "'{0}'",
        "domain": "youtube.part",
        "reason": "unknownPart",
        "location": "part",
        "locationType": "parameter"
      }
    ]
  }
}

Unfortunately, this error doesn’t explain anything, and I couldn’t find anything to help me to solve it. I hope someone can explain what is going on here.

I put all relative files down below and added some comments.

web.php

Route::get('youtube/{task}', [YoutubeController::class, 'authenticate'])->name('youtube.authenticate');
Route::get('youtube/{task}/redirect', [YoutubeController::class, 'create'])->name('youtube.create');

YoutubeController.php

class YoutubeController extends Controller
{
    private $youtube;

    public function __construct(Request $request)
    {
        // like YoutubeStreamService or YoutubeUploadService
        $this->youtube = new ("AppServicesYoutubeYoutube" . ucfirst($request->route()->parameter('task')) . "Service");
    }

    public function authenticate($task)
    {
        return redirect()->away($this->youtube->authenticate($task));
    }

    public function create(Request $request, $task)
    {
        $this->youtube->create($request, $task);
    }
}

I use an abstract class for authentication codes.

abstract class YoutubeAbstraction
{
    // Called from the controller.
    // Returns the url to google to authenticate the request. 
    public function authenticate($task)
    {
        return $this->client($task)->createAuthUrl();
    }

    // This code came from mostly Youtueb API documentation.
    protected function client($task)
    {
        $scopes = [
            'upload' => ['https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube.force-ssl'],
            'stream' => ['https://www.googleapis.com/auth/youtube.force-ssl']
        ][$task];
        $client = new Google_Client();
        $client->setApplicationName("MyApp");
        $client->setScopes($scopes);
        $client->setAuthConfig(base_path("client_secret_{$task}.json"));
        $client->setAccessType('offline');

        return $client;
    }

    abstract public function create($request, $task); 
}

YoutubeStreamService.php

class YoutubeStreamService extends YoutubeAbstraction
{
    // This code came from Youtube API documentation completely.
    // It contains only the required fields and their hard-coded values.
    public function create($request, $task)
    {
        $client = $this->client($task);
        $client->setAccessToken($client->fetchAccessTokenWithAuthCode($request->code));

        $service = new Google_Service_YouTube($client);        
        $liveBroadcast = new Google_Service_YouTube_LiveBroadcast();

        $liveBroadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
        $liveBroadcastSnippet->setTitle('my title');
        $liveBroadcastSnippet->setScheduledStartTime('2021-04-04T20:00:00.00+03:00');
        $liveBroadcast->setSnippet($liveBroadcastSnippet);

        $liveBroadcastStatus = new Google_Service_YouTube_LiveBroadcastStatus();
        $liveBroadcastStatus->setPrivacyStatus('private');
        $liveBroadcast->setStatus($liveBroadcastStatus);

        // If I add dd($liveBroadcast) here, I see the object.
        // So the error is thrown by the function down below.

        $response = $service->liveBroadcasts->insert('', $liveBroadcast);
        print_r($response);
    }
}

Answer

As per the official specification, your call to the LiveBroadcasts.insert API endpoint has to include the request parameter:

part (string)

The part parameter serves two purposes in this operation. It identifies the properties that the write operation will set as well as the properties that the API response will include.

The part properties that you can include in the parameter value are id, snippet, contentDetails, and status.

In PHP, that requirement boils down to having your API call like the one below:

$response = $service->liveBroadcasts->insert(
    'id,snippet,status', $liveBroadcast);


Source: stackoverflow