首页 > 解决方案 > 我从 youtube-api 收到“500 后端错误”和“400 无效元数据”

问题描述

我使用 PHP 通过 youtube-api 插入/更新视频的标题和描述,但我遇到了一些问题。

1. 当我使用 zh-Hant 或 zh-Hans 插入/更新视频的标题和描述时,我得到"500 Backend Error." 但是当我更改zh-Hantzh-Hans时它可以工作zh-twzh-cn

500 后端错误

在此处输入图像描述

2."400 invalidMetadata"当我使用意大利语(它)更新视频标题时得到。我对此一无所知,因为在其他语言中一切都很好,而且在使用意大利语更新视频描述时也很好。

400 无效元数据

在此处输入图像描述

我该如何解决这些问题?

这是我的代码:

public function __construct()
{
    $client = new \Google_Client();
    $client->setAuthConfig(storage_path('key/youtube_key/client_secret.json'));
    $client->setApplicationName("String System");
    $client->setScopes('https://www.googleapis.com/auth/youtube.force-ssl');
    $client->setAccessType('offline');
    $client->setAccessToken(Cache::get('google_auth_token'));

    $this->client = $client;
}

public function youtubeService($stringKey, $stringValue, $language)
{
    $this->service = new \Google_Service_YouTube($this->client);

    $this->stringKey = $stringKey;
    $this->stringValue = $stringValue;
    $this->language = $language;

    $key = explode('_', $this->stringKey);
    if (3 != count($key)) {
        return;
    }

    list($videoName, $videoID, $type) = $key;
    $this->videoName= $videoName;
    $this->videoID = $videoID;

    switch ($type) {
        case 'title':
            $this->updateTitle();
            break;

        case 'description':
            $this->updateDesc();
            break;
    }

    return;
}

private function updateTitle()
{
    // get video list by video ID
    $video = $this->service->videos->listVideos(
        'snippet,localizations',
        ['id' => $this->videoID]
    );
    $videoInfo = $video->items[0];

    // set video title of language
    if (isset($videoInfo->localizations[$this->language])) {
        $videoInfo->localizations[$this->language]->title =
                $this->stringValue;

        // check default language
        if ($this->language === $videoInfo->snippet->defaultLanguage) {
            $videoInfo->snippet->title = $this->stringValue;
        }
    } else {
        $videoInfo->localizations[$this->language] = [
            'title' => $this->stringValue,
            'description' => 'description'
        ];
    }

    try {
        // update video information
        $this->service->videos->update(
            'snippet,localizations',
            $videoInfo
        );
    } catch (Exception $e) {
        // do nothing and continue
    }

    return;
}

private function updateDesc()
{
    // get video list by video ID
    $video = $this->service->videos->listVideos(
        'snippet,localizations',
        ['id' => $this->videoID]
    );
    $videoInfo = $video->items[0];

    // set video description of language
    if (isset($videoInfo->localizations[$this->language])) {
        $videoInfo->localizations[$this->language]->description =
                $this->stringValue;

        // check default language
        if ($this->language === $videoInfo->snippet->defaultLanguage) {
            $videoInfo->snippet->description = $this->stringValue;
        }
    } else {
        $videoInfo->localizations[$this->language] = [
            'title' => 'title',
            'description' => $this->stringValue
        ];
    }

    try {
        // update video information
        $this->service->videos->update(
            'snippet,localizations',
            $videoInfo
        );
    } catch (Exception $e) {
        // do nothing and continue
    }

    return;
}

标签: phpgoogle-apigoogle-oauthyoutube-data-apigoogle-api-php-client

解决方案


目前似乎不支持您使用的语言(zh-Hant & zh-Hans)。

要验证是否支持某种语言,您可以使用 i18nLanguages.list API。这是使用 API 资源管理器的示例请求。

https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.i18nLanguages.list?part=snippet&_h=1&


推荐阅读