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.


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


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' => ['', ''],
            'stream' => ['']
        $client = new Google_Client();

        return $client;

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


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);

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

        $liveBroadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
        $liveBroadcastSnippet->setTitle('my title');

        $liveBroadcastStatus = new Google_Service_YouTube_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);


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