0

I have tried uploading a video to YouTube channels using by the YouTube Data API, by composer:

    "require": {
        "google/apiclient": "^2.7",
        "hybridauth/hybridauth": "~3.4.0",
        "guzzlehttp/guzzle": "^7.0"
    }

The video is uploaded successfully, but YouTube marks every video uploaded into it as private. Does anybody know how to fix this scenario?

    public function upload_video_on_youtube($id, $arr_data)
    {
        $result_data = array();
        $channel_id = $id;
        $uploaded = false;
        $stopper = 0;
        while ($uploaded == false && $stopper == 0) {
            $arr_data['summary'] = $this->getrandomstring(10);
            $arr_data['title'] = $this->getrandomstring(10);
            $client = new Google_Client();
            $arr_token = $this->getAccessToken($channel_id);
            if ($arr_token['error'] == false) {
                $res = array();
                $accessToken = array(
                    'access_token' => $arr_token['access_token']
                );
                $client->setAccessToken($accessToken);
                $service = new Google_Service_YouTube($client);
                $video = new Google_Service_YouTube_Video();
                $videoSnippet = new Google_Service_YouTube_VideoSnippet();
                $videoSnippet->setDescription($arr_data['summary']);
                $videoSnippet->setTitle($arr_data['title']);
                $video->setSnippet($videoSnippet);
                $videoStatus = new Google_Service_YouTube_VideoStatus();
                $videoStatus->setPrivacyStatus('unlisted');
                $video->setStatus($videoStatus);
                try {
                    $response = $service->videos->insert(
                        'snippet,status',
                        $video,
                        array(
                            'data' => file_get_contents($arr_data['video_path']),
                            'mimeType' => 'video/*',
                            'uploadType' => 'multipart'
                        )
                    );
                    if (isset($response->id)) {
                        $video_id = $response->id;
                        $res['error'] = false;
                        $res['response'] = $video_id;
                        array_push($result_data, $res);
                        $uploaded = true;
                        return $result_data;
                    }
                } catch (Exception $e) {
                    if (401 == $e->getCode()) {
                        // echo ($arr_token['email'] . " Youtube  Access token expired");
                        $refresh_token = $this->get_refersh_token($channel_id);
                        $client = new GuzzleHttp\Client(['base_uri' => 'https://accounts.google.com']);
                        $response = $client->request('POST', '/o/oauth2/token', [
                            'form_params' => [
                                "grant_type" => "refresh_token",
                                "refresh_token" => $refresh_token,
                                "client_id" => $arr_token['client_id'],
                                "client_secret" => $arr_token['client_secret'],
                            ],
                        ]);
                        $data = json_decode($response->getBody());
                        $data->refresh_token = $refresh_token;
                        $this->update_access_token($channel_id, json_encode($data));
                        $uploaded = false;
                    } elseif (403 == $e->getCode()) {
                        // echo ($arr_token['email'] . '  Youtube channel quota exceeded');
                        $channel_id = $channel_id + 1;
                        $uploaded = false;
                    }
                }
            } else if ($arr_token['error'] == true) {
                $res['error'] = true;
                $res['response'] = "Your Daily Upload Quota is Exceeded";
                array_push($result_data, $res);
                $stopper = 1;
                return $result_data;
            }
        }
    }
stvar
  • 6,551
  • 2
  • 13
  • 28
JoeL
  • 1
  • 1
    This is the answer to your question: [Using Youtube Data API makes my videos private on upload](https://stackoverflow.com/a/64080239/8327971). – stvar Dec 10 '20 at 19:08

1 Answers1

1

If you check the documentation for videos.insert you will see that all videos that are uploaded by apps that have not gone through the verification process will be set to private.

Once you go through the verification process you will be able to set your videos to public

Vide

This is an update after some clarity from Google.

At the top of the Video.insert page you will see it states.

Apps that dont need to be verified can just go though an audit is not verification, these are two different things. You need to apply for an audit. YouTube API Services - Audit and Quota Extension Form

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • actually i am storing the clientid,client secret,auth key,refresh token after all verification with oath playground as redirect url..Then i am just using videos.insert method with this access token ..but still they mark it as private – JoeL Dec 29 '20 at 04:16
  • Well theirs your problem dont use Oauth playground to generate your refresh tokens. Thats only for testing and will still only allow for uploading private videos. You will need your own project and then you will need to go though the verification process then you will be able to upload public videos. – Linda Lawton - DaImTo Dec 29 '20 at 12:55
  • is there any way to upload videos with only one time varification of project .I need clients should not aware about video uploading is to youtube. – JoeL Jan 07 '21 at 16:45
  • Oauth2 is the only method supported for authorizing requests to the YouTube api. You need the users permission in order to upload data to their account. – Linda Lawton - DaImTo Jan 07 '21 at 20:11