问题
I've looked at Youtube's documentation but I don't seem to understand how to create a playlist for the user specifically for ios. I know the user needs to sign in using OAuth 2 to grant apps access/authority to create a playlist
Documentation link: https://developers.google.com/youtube/v3/sample_requests#uploaded_videos
But then given this code:
POST {base_URL}/playlists?part=snippet
Request body:
{
'snippet': {
'title': 'New playlist',
'description': 'Sample playlist for Data API',
}
}
I'm not sure how to translate this to work on ios. How would I reflect the Request body object in objective c?
-------Update----- Would it just be using NSURLSessionUploadTask? so I can send a post request and also send a dictionary for the request body? sry, a bit new to the IOS space
回答1:
Here is update!
Client library from Google was updated.
Documentation: YouTube Data API Overview
Playlist documentation: Playlists: insert
GitHub client library: Google APIs Client Library for Objective-C For REST
Here are pods need to be install:
pod 'GTMSessionFetcher'
pod 'GTMOAuth2'
pod 'GoogleAPIClientForREST/YouTube’
If we think that we have clientID, clientSecret and scope (read documentation) then we can create viewController for authentication:
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];
Method that creates private playlist:
- (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:
You are correct, you need to grant apps access to create a playlist by using OAuth 2.0. It is generally a best practice to request scopes incrementally at the time access is required rather than up front.
Here's a code snippet how to create a playlist via YouTube API in IOS:
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);
来源:https://stackoverflow.com/questions/36927062/create-playlist-in-youtube-api