ios - 在 Youtube api 中创建播放列表

标签 ios youtube youtube-api youtube-data-api

我看过 Youtube 的文档,但我似乎不明白如何专门为 ios 用户创建播放列表。我知道用户需要使用 OAuth 2 登录才能授予应用访问权限/创建播放列表的权限

文档链接:https://developers.google.com/youtube/v3/sample_requests#uploaded_videos

但是给出这段代码:

POST {base_URL}/playlists?part=snippet
 Request body:
{
'snippet': {
  'title': 'New playlist', 
  'description': 'Sample playlist for Data API',
 }
}

我不确定如何将其翻译成适用于 ios。我将如何在 objective-c 中反射(reflect)请求正文对象?

--------更新----- 它只是使用 NSURLSessionUploadTask 吗?这样我就可以发送一个发布请求并为请求正文发送一个字典?抱歉,对 IOS 空间有点陌生

最佳答案

这里是更新!

Google 的客户端库已更新。

文档:YouTube Data API Overview

播放列表文档:Playlists: insert

GitHub 客户端库:Google APIs Client Library for Objective-C For REST

这里是需要安装的pod:

pod 'GTMSessionFetcher'
pod 'GTMOAuth2'
pod 'GoogleAPIClientForREST/YouTube’

如果我们认为我们有 clientID、clientSecret 和范围(阅读文档),那么我们可以创建 viewController 进行身份验证:

GTMOAuth2ViewControllerTouch *viewController = [[GTMOAuth2ViewControllerTouch alloc]
                                                    initWithScope:scope
                                                    clientID:kMyClientID
                                                    clientSecret:kMyClientSecret
                                                    keychainItemName: nil
                                                    completionHandler:
                                                    ^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) {
                                                        [self dismissViewControllerAnimated:NO completion:nil];
                                                        if (!error)
                                                        {
                                                            GTLRYouTubeService *youTubeService = [[GTLRYouTubeService alloc] init];
                                                            youTubeService.authorizer = auth;
                                                            [self createPlaylistWithTitle: @"Title" description: @"description" youtubeService: youtubeService];
                                                        }
                                                    }];
[self presentViewController:viewController animated:YES completion:nil];

创建私有(private)播放列表的方法:

- (void)createPlaylistWithTitle:(NSString *)playlistTitle description:(NSString *)playlistDescription youtubeService:(GTLRYouTubeService *)youTubeService
{
    GTLRYouTube_Playlist *playlist = [[GTLRYouTube_Playlist alloc] init];

    GTLRYouTube_PlaylistSnippet *playlistSnippet = [[GTLRYouTube_PlaylistSnippet alloc] init];
    playlistSnippet.title = playlistTitle;
    playlistSnippet.descriptionProperty = playlistDescription;

    GTLRYouTube_PlaylistStatus *playlistStatus = [[GTLRYouTube_PlaylistStatus alloc] init];
    playlistStatus.privacyStatus = @"private";

    playlist.snippet = playlistSnippet;
    playlist.status = playlistStatus;

    GTLRYouTubeQuery_PlaylistsInsert *query = [GTLRYouTubeQuery_PlaylistsInsert queryWithObject:playlist part:@"snippet,status"];
    [youTubeService executeQuery:query completionHandler:^(GTLRServiceTicket *ticket, id object, NSError *error) {
        if (!error)
        {
            NSLog(@"response: %@", object);
        }
    }];
}

关于ios - 在 Youtube api 中创建播放列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36927062/

相关文章:

ios - 状态栏是横向的,但是 [[UIApplication sharedApplication] statusBarOrientation] 返回纵向

ios - 如何在 Gamesalad for iOS 中制作通用应用程序?

php - PHP:如何使用一个_GET值和另一个设置的数组创建数组

swift - 在 Swift 中将视频上传到 YouTube

android - Android 上的 Youtube API Player 每 2 秒自动暂停一次

android - Android 版 YouTube 播放器在播放广告时崩溃

ios - 启用 guard malloc 时出现奇怪的错误

ios - JSON 解析返回 null

webview - Android Marshmallow 在后台流式传输(缓冲区?)

python - GData Youtube api 慢