我已经查看了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中反映Request body对象?
------- -----更新 它只是使用NSURLSessionUploadTask吗?所以我可以发送一个帖子请求,并发送请求正文的字典? sry,IOS空间有点新鲜
答案 0 :(得分:1)
Google的客户端库已更新。
播放列表文档: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);
}
}];
}
答案 1 :(得分:0)
您是对的,您需要授予应用访问权限才能使用OAuth 2.0创建播放列表。通常最佳做法是在需要访问而不是预先访问时逐步请求范围。
以下是如何在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);