In this tutorial I will be covering how to set up and use the youtube api for uploading video using codeigniter framework without user authentication . When a user try to upload video in youtube through your site then their is two way arise,one is user authentication another is without user authentication . But the preferable way is the second way means without user authentication and also there is a benefit that all the videos will be uploaded in your youtube channel . So lets start and follow my step .
First of all you need to access the youtube api for getting the client id and client secret . So go to the https://console.developers.google.com and create a project . Then take the client id & client secret . And set the redirect uri .
Now we need an access token and a refresh token for uploading video without user authentication . So just visit to this link https://developers.google.com/oauthplayground/ and get the access token and refresh token from here .
Now you have to download a CI library from github .So visit this link https://github.com/utpalkoley/upload_video_in_youtube_using_codeigniter and download the full library .
Now go to this application/config/ and edit the Google.php with your client id,secret id and redirect uri .
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); $config['OAUTH2_CLIENT_ID'] = 'YOUR_CLIENT_ID_HERE'; //your auth 2.0 client id $config['OAUTH2_CLIENT_SECRET'] = 'YOUR_CLIENT_SECRET_HERE';//your auth 2,0 client secret $config['REDIRECT_URI'] = 'http://www.YOUR_DOMAIN.com/welcome/youtube'; //Authorized redirect URIs a callback url which you should save in google console
Then make a text file name it the_key.txt and place it into the asset folder . The access token valid for a limited time . That’s why we need to generate access token using refresh token and store it into this text file .
When you start uploading the video first the video will upload into a server folder which is into the root path called ‘upload’ . Then it will be transferred into your youtube channel by the bellow code .
$OAUTH2_CLIENT_ID = $this->ci->config->item('OAUTH2_CLIENT_ID'); $OAUTH2_CLIENT_SECRET = $this->ci->config->item('OAUTH2_CLIENT_SECRET'); $key = file_get_contents('./asset/the_key.txt'); $client = new Google_Client(); $client->setApplicationName("abcd");//give your project name which is created by you in to the google console developer $client->setClientId($OAUTH2_CLIENT_ID); $client->setAccessType('offline'); //print_r($client->setAccessToken($key));die; $client->setClientSecret($OAUTH2_CLIENT_SECRET); $client->setScopes('https://www.googleapis.com/auth/youtube'); $redirect = $this->ci->config->item('REDIRECT_URI'); $client->setRedirectUri($redirect); $refreshToken = "your refresh token which you have gotten from the google authplaygorund"; $client->refreshToken($refreshToken); $_SESSION['access_token'] = $client->getAccessToken(); if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { $youtube = new Google_Service_YouTube($client); try{ realpath(APPPATH . 'asset/videos/Whatsapp.mp4'); $videoPath = $video; $snippet = new Google_Service_YouTube_VideoSnippet(); $snippet->setTitle($title); $snippet->setDescription($desc); $snippet->setTags($tags); $snippet->setCategoryId("22"); $status = new Google_Service_YouTube_VideoStatus(); $status->privacyStatus = $privacy_status; $video = new Google_Service_YouTube_Video(); $video->setSnippet($snippet); $video->setStatus($status); $chunkSizeBytes = 1 * 1024 * 1024; $client->setDefer(true); $insertRequest = $youtube->videos->insert("status,snippet", $video); $media = new Google_Http_MediaFileUpload( $client, $insertRequest, 'video/*', null, true, $chunkSizeBytes ); $media->setFileSize(filesize($videoPath)); $status = false; $handle = fopen($videoPath, "rb"); while (!$status && !feof($handle)) { $chunk = fread($handle, $chunkSizeBytes); $status = $media->nextChunk($chunk); } fclose($handle); $client->setDefer(false);
You should edit this code that’s why go to the application/libraries/ and edit the Google_client_api.php file . Just a silly change for you that
- change the application name
- add your refresh token
Now check the code by uploading a video . If everything is okay then it will be uploaded successfully . Otherwise you can ask me any kind of question by putting a comment in the bellow . I am always available to answer you .
Have you authorized your channel??
Hi Subrata. I am trying to implement this code but i couldn’t. Please help me. I am getting Error. “the OAuth2 token, message: ‘{ “error” : “invalid_request”, “error_description” : “Could not determine client ID from request.” }’ I am little bit confuse while confuring for Oauthplayground.
1.select youtube Data Api v3.
2.select for ….youtube.upload.
3. Click on exchange autorization code for tokens
4.Then get refresh token
I changed the project name and refresh token. but what next now?
Thank you,It’s work for me.I just followed step by step.It’s so nice that all of the video’s are now upload into the same channel.
Hi Darshan,I think there is a problem regarding your client id.Create a new project in google api.Then watch this video:”https://www.youtube.com/watch?v=hfWe1gPCnzc”.And generate the new refesh token.
error Maximum execution time of 300 seconds exceeded
video upload on floder but give me that error
Fatal error: Maximum execution time of 300 seconds exceeded in C:\xampp\htdocs\upload_video\application\libraries\google-api-php-client\src\Google\IO\Curl.php on line 110
An uncaught Exception was encountered
Type: Google_Auth_Exception
Message: Error refreshing the OAuth2 token, message: ‘{ “error” : “unauthorized_client” }’
Filename: C:\xampp\htdocs\webtv\application\libraries\google-api-php-client\src\Google\Auth\OAuth2.php
Line Number: 357
Backtrace:
File: C:\xampp\htdocs\webtv\application\libraries\google-api-php-client\src\Google\Auth\OAuth2.php
Line: 270
Function: refreshTokenRequest
File: C:\xampp\htdocs\webtv\application\libraries\google-api-php-client\src\Google\Client.php
Line: 454
Function: refreshToken
File: C:\xampp\htdocs\webtv\application\libraries\Google_client_api.php
Line: 40
Function: refreshToken
File: C:\xampp\htdocs\webtv\application\controllers\Welcome.php
Line: 41
Function: youtube_upload
File: C:\xampp\htdocs\webtv\index.php
Line: 315
Function: require_once
i have this problem please solve it
Hey I used your library and it successfully uploaded the video to my channel after some glitches. But my video is not visible on the channel. In the YouTube studio it shows processing. Been like that for 24 hours Can you tell what’s the problem? I followed this step by step.