Coder Social home page Coder Social logo

youtube's Introduction

Laravel 5 - YouTube Video Upload

Please note, that this package will only work with a single YouTube account and does not support multiple accounts.

Installation

To install, use the following to pull the package in via Composer.

composer require dawson/youtube

Now register the Service provider in config/app.php

'providers' => [
    ...
    'Dawson\Youtube\YoutubeServiceProvider',
],

And also add the alias to the same file.

'aliases' => [
    ...
    'Youtube' => 'Dawson\Youtube\Facades\Youtube',
],

Configuration

You now need to publish the youtube.php config and migrations.

php artisan vendor:publish --provider="Dawson\Youtube\YoutubeServiceProvider"

Now you'll want to run php artisan migrate to create the youtube_access_tokens table which as you would imagine, will contain your access tokens once you're authenticated correctly.

Obtaining your Credentials

If you haven't already, you'll need to create an application on Google's Developer Console. You then need to head into Credentials within the Console to create Server key.

You will be asked to enter your Authorised redirect URIs. When installing this package, the default redirect URI is http://laravel.dev/youtube/callback. Of course, replacing the domain (laravel.dev) with your applications domain name.

You can add multiple redirect URIs, for example you may want to add the URIs for your local, staging and production servers.

Once you are happy with everything, create the credentials and you will be provided with a Client ID and Client Secret. These now need to be added to your .env file.

GOOGLE_CLIENT_ID=YOUR_CLIENT_ID
GOOGLE_CLIENT_SECRET=YOUR_SECRET

Authentication

Now your application is configured, we'll go through the inital authentication with Google. By default, the authorization route is /youtube/auth. Simply visit this URI in your application and you will be redirect to Google to authenticate your YouTube account.

Assuming you were not presented with any errors during authentication, you will be redirected back to your application root. (/).

Reviewing your Token

Previously, users of this package have reported issues with their access token(s). To ensure you have the correct token, you simply need to review the youtube_access_tokens table you migrated earlier and review the value in the access_token column.

**You need to check that a refresh_token exists within this value. If this is correct, you're all set to begin uploading. **

Upload a Video

To upload a video, you simply need to pass the full path to your video you wish to upload and specify your video information.

Here's an example:

$video = Youtube::upload($pathToVideo, [
    'title'       => 'My Awesome Video',
    'description' => 'You can also specify your video description here.',
    'tags'	      => ['foo', 'bar', 'baz'],
    'category_id' => 10
]);

return $video->getVideoId();

The above will return the ID of the uploaded video to YouTube. (i.e dQw4w9WgXcQ)

By default, video uploads are public. If you would like to change the privacy of the upload, you can do so by passing a third parameter to the upload method.

For example, the below will upload the video as unlisted.

$video = Youtube::upload($pathToVideo, $params, 'unlisted');

Custom Thumbnail

If you would like to set a custom thumbnail for for upload, you can use the withThumbnail() method via chaining.

$pathToImage = storage_path('app/public/thumbnail.jpg');

$video = Youtube::upload($pathToVideo, $params)->withThumbnail($pathToImage);

return $youtube->getThumbnailUrl();

Please note, the maxiumum filesize for the thumbnail is 2MB. Setting a thumbnail will not work if you attempt to use a thumbnail that exceeds this size.

Deleting a Video

If you would like to delete a video, which of course is uploaded to your authorized channel, you will also have the ability to delete it:

Youtube::delete($videoId);

When deleting a video, it will check if exists before attempting to delete.

Questions

Should you have any questions, please feel free to submit an issue.

youtube's People

Contributors

ahmedash95 avatar arcanedev-maroc avatar izal avatar joedawson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.