【问题标题】:Youtube Data API UnknowPart Error When Create Broadcasting创建广播时出现 Youtube 数据 API UnknowPart 错误
【发布时间】:2021-06-30 17:13:33
【问题描述】:

我正在尝试通过 Youtube Data API 通过我的网页创建 Youtube 直播。无论我尝试了什么,都会不断出现该错误:

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

不幸的是,这个错误并不能解释任何事情,我找不到任何可以帮助我解决它的东西。我希望有人能解释这里发生了什么。

我把所有相关文件放在下面并添加了一些 cmets。

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 ("\App\Services\Youtube\Youtube" . 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);
    }
}

我使用一个抽象类作为验证码。

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

【问题讨论】:

    标签: php laravel youtube-api youtube-data-api


    【解决方案1】:

    根据官方规范,您对LiveBroadcasts.insert API 端点的调用必须包含请求参数:

    part(字符串)

    part 参数在此操作中有两个用途。它标识了写入操作将设置的属性以及 API 响应将包含的属性。

    您可以在参数值中包含的part properties 是idsnippetcontentDetailsstatus

    在 PHP 中,这一要求归结为让您的 API 调用如下所示:

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

    【讨论】:

    • 谢谢 stvar,我快要疯了。
    猜你喜欢
    • 2022-06-14
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2017-04-30
    • 2021-10-15
    • 2017-01-05
    • 2021-01-30
    相关资源
    最近更新 更多