0

I use PHP to insert/update the title and description of our video by using youtube-api and I get some problems with it.

1. When I insert/update title and description of video by using zh-Hant or zh-Hans, I get "500 Backend Error." However it works when I change zh-Hant and zh-Hans to zh-tw and zh-cn.

500 Backend Error

enter image description here

2. I get "400 invalidMetadata" when I update a video title by using Italian(it). I have no idea about this because it's all fine in others languages and it also fine in update video description by using Italian.

400 invalidMetadata

enter image description here

How can I fix these problems?

Here is my code:

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;
}
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
ws6125
  • 1
  • 1

1 Answers1

0

It seems that the language that you used (zh-Hant & zh-Hans) is not supported as of the moment.

To verify whether a language is supported or not, you may utilize i18nLanguages.list API. Here's a sample request using API explorer.

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

goblin
  • 1,513
  • 13
  • 13
  • Hello, thanks for your answer. This solve a my first problem. But, the second is uncanny because "it" throws error of "invalidMetadata" to me. How can I solve this problem? – ws6125 Jul 03 '19 at 09:15