【问题标题】:create playlist in Youtube api在 Youtube api 中创建播放列表
【发布时间】:2016-08-23 22:47:04
【问题描述】:

我查看了 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。我将如何在目标 c 中反映请求正文对象?

--------更新----- 它只是使用 NSURLSessionUploadTask 吗?所以我可以发送一个帖子请求,也可以为请求正文发送一个字典?不好意思,对IOS空间有点新意

【问题讨论】:

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


    【解决方案1】:

    这里是更新!

    来自 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 和 scope(阅读文档),那么我们可以创建 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];
    

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

    - (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);
            }
        }];
    }
    

    【讨论】:

      【解决方案2】:

      您是对的,您需要使用OAuth 2.0 授予应用程序访问权限以创建播放列表。通常最好的做法是在需要访问时增量请求范围,而不是预先请求。

      这里有一段代码 sn-p 如何在 IOS 中通过 YouTube API 创建播放列表:

      GTMOAuth2ViewControllerTouch *viewController = [[GTMOAuth2ViewControllerTouch alloc]
      initWithScope:scope
      clientID:clientId
      clientSecret:clientSecret
      keychainItemName:keychainItemName
      completionHandler:
      ^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) {
      if (error) {
      [SVProgressHUD showErrorWithStatus:error.localizedDescription];
      } else {
      GTLServiceYouTube *service = [FZMYoutubeSearchService sharedYoutubeService];
      service.authorizer = auth;
      
      GTLYouTubePlaylist *playlist = [[GTLYouTubePlaylist alloc] init];
      
      GTLYouTubePlaylistSnippet *playlistSnippet = [[GTLYouTubePlaylistSnippet alloc] init];
      playlistSnippet.title = @"this is my great playlist";
      playlistSnippet.descriptionProperty = @"and this is description";
      
      GTLYouTubePlaylistStatus *playlistStatus = [[GTLYouTubePlaylistStatus alloc] init];
      playlistStatus.privacyStatus = @"private";
      
      playlist.snippet = playlistSnippet;
      playlist.status = playlistStatus;
      
      GTLQueryYouTube *query = [GTLQueryYouTube    queryForPlaylistsInsertWithObject:playlist part:@"snippet,status"];
      [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket,      id object, NSError *error) {
      if (error) {
      NSLog(@"error: %@", error);
      

      【讨论】:

      • FZMYoutubeSearchService 和 GTLYouTubePlaylist 是从哪里来的?我似乎无法在网上找到它们
      猜你喜欢
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 2018-01-15
      • 2020-01-23
      • 1970-01-01
      • 2017-03-15
      相关资源
      最近更新 更多