我看过 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 的客户端库已更新。
播放列表文档: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/