我从youtube-api中收到“ 500后端错误”和“ 400 invalidMetadata”

时间:2019-03-07 06:06:23

标签: php google-api google-oauth youtube-data-api google-api-php-client

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

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

500 Backend Error

enter image description here

2。 当我使用Italian(it)更新视频标题时,我得到"400 invalidMetadata"。 我对此一无所知,因为在其他语言中都可以,并且在使用意大利语更新视频说明中也可以。

400 invalidMetadata

enter image description here

如何解决这些问题?

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

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

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

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