I would like to know if there is any method or api to upload videos to youtube directly from my flutter application? One way would be to use webview, but it's not so comfortable and user-friendly. Thank You very much for any suggestions
Use googleapis
Youtube Data:
static Future<Video> upload() async {
var youTubeApi = await getYoutubeApi();
File f = File('assets/sample-mp4-file-small.mp4');
Stream<List<int>> stream = f.openRead();
Media m = Media(stream, (await f.length()));
Video video = Video(
snippet: VideoSnippet(
title: 'Pong Map Test Video',
description: 'Test Upload for Pong Map app',
categoryId: '22',
),
);
return await youTubeApi.videos.insert(
video,
['snippet', 'status'],
uploadMedia: m,
);
}
import 'dart:io';
import 'package:googleapis/youtube/v3.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';
import 'package:shared/services/toast_utility.dart';
class YoutubeService {
static Future<YouTubeApi> getYoutubeApi() async {
final GoogleSignIn googleSignIn = GoogleSignIn(
scopes: <String>[
YouTubeApi.youtubeReadonlyScope,
YouTubeApi.youtubeUploadScope
],
);
await googleSignIn.signIn();
// final GoogleSignInAccount? googleSignInAccount = await googleSignIn.signIn();
var httpClient = await googleSignIn.authenticatedClient();
if (httpClient == null) {
print("You didn't allow to proceed with YouTube access");
}
return YouTubeApi(httpClient!);
}
static Future<VideoListResponse> listYoutubePlaylists() async {
var youTubeApi = await getYoutubeApi();
var data = await youTubeApi.videos.list(
['snippet'],
chart: "mostPopular",
);
return data;
}
static Future<Video> upload() async {
var youTubeApi = await getYoutubeApi();
File f = File('assets/sample-mp4-file-small.mp4');
Stream<List<int>> stream = f.openRead();
Media m = Media(stream, (await f.length()));
Video video = Video(
snippet: VideoSnippet(
title: 'Test Video',
description: 'Test Upload for My App',
categoryId: '22',
),
);
return await youTubeApi.videos.insert(
video,
['snippet', 'status'],
uploadMedia: m,
);
}
}
Note that you do need extension_google_sign_in_as_googleapis_auth
and google_sign_in
both in your pubspec.yaml
for authorization. And of course the googleapis
package.
Google provides API tools for YouTube. You can learn more here.
Furthermore, there is a package that implements that same api in a more friendly way, as shown here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With