2012-12-11 182 views
4

您好,我試圖使用xcode中的google-api庫從youtube API獲取頻道對象,這是我正在使用的代碼:使用API​​的v3在Objective-C中檢索YouTube頻道上傳使用API​​的v3獲取YouTube頻道上傳

//youtubeService type is GTLServiceYouTube 
    youtubeService = [[GTLServiceYouTube alloc] init]; 
    youtubeService.shouldFetchNextPages = YES; 
    //auth is the object from the authentication callback 
    youtubeService.authorizer = auth; 
    videoQuery = [GTLQueryYouTube 
    queryForChannelsListWithPart:@"RayWilliamJohnson"]; 
    videoQuery.maxResults = 20; 

    youtubeTicket = [youtubeService executeQuery:videoQuery completionHandler: 
^(GTLServiceTicket *ticket, id channel, NSError *error) { 
       NSLog(@" %@ ", error.description); 
       NSLog(@"WIN! : %@ ", channel); 
       }]; 

到目前爲止,我已經收到此錯誤:

Error Domain=com.google.GTLJSONRPCErrorDomain Code=-32602 "The operation couldn’t be completed. (RayWilliamJohnson)" UserInfo=0x7532c40 {error=RayWilliamJohnson, GTLStructuredError=GTLErrorObject 0x7533d30: {message:"RayWilliamJohnson" code:-32602 data:[1]}, NSLocalizedFailureReason=(RayWilliamJohnson)}

任何想法拋出這個錯誤?

在此先感謝您的幫助!

編輯: 我讀過的文件中,根本「不存在」的類:P並檢查也遵循同樣的做法與其他例子。

EDIT2:好,我嘗試了以下的變化和他們沒有工作:(,它使用的查詢瀏覽器相同的例子爲我工作

videoQuery.maxResults = 20; 
videoQuery.q = @"RayWilliamJohnson"; 
videoQuery.type = @"channel"; 

/*tried setting the part without calling the query 
method on GTLQueryYoutube but that did not work videoQuery.part = @"id";*/ 

videoQuery = [GTLQueryYouTube queryForChannelsListWithPart:@"id"]; 
//fetching only 20 items. 
youtubeTicket = [youtubeService executeQuery:videoQuery completionHandler: 
^(GTLServiceTicket *ticket, id channel, NSError *error) { 
    NSLog(@" %@ ", error.description); 
    NSLog(@"WIN! : %@ ", channel); 
    }]; 

錯誤略有不同。它說,我還沒有指定任何過濾器,即使我做了

Error Domain=com.google.GTLJSONRPCErrorDomain Code=-32602 "The operation couldn’t be completed. (No filter selected.)" UserInfo=0x8870250 {error=No filter selected., GTLStructuredError=GTLErrorObject 0x88707f0: {message:"No filter selected." code:-32602 data:[1]}, NSLocalizedFailureReason=(No filter selected.)} 

EDIT3: 好,我已經改變了幾件事情,第一我是在製造一個愚蠢的錯誤,我設置參數,然後簡單地刪除它們之後初始化我的查詢。所以代碼現在看起來像這樣:

 youtubeService.authorizer = auth; 
     videoQuery = [GTLQueryYouTube queryForChannelsListWithPart:@"id"]; 
     videoQuery.maxResults = 1; 
     videoQuery.q = @"RayWilliamJohnson"; 
     videoQuery.type = @"channel"; 


     youtubeTicket = [youtubeService executeQuery:videoQuery 
         completionHandler:^(GTLServiceTicket *ticket, GTLYouTubeChannel *channel, 
              NSError *error) { 
      NSLog(@" %@ ", error.description); 
      NSLog(@"WIN! : %@ ", channel.description); 
      }]; 

,但我仍然得到的「沒有過濾器指定」,這意味着變量沒有設置任何一種方式的錯誤...

Edit4 :經過如此多的金髮時刻後,我設法做到這一點,它是我正在初始化的查詢對象,我試圖使用專門用於頻道查找的前綴,我應該使用queryForSearchListWithPart,所以工作的代碼如下所示:

youtubeService.authorizer = auth;  
videoQuery = [GTLQueryYouTube queryForSearchListWithPart:@"id"]; 
videoQuery.maxResults = 1; 
videoQuery.q = @"RayWilliamJohnson";  
youtubeTicket = [youtubeService executeQuery:videoQuery 
     completionHandler:^(GTLServiceTicket *ticket, GTLYouTubeChannel *channel, 
      NSError *error) { 
      NSLog(@" %@ ", error.description); 
      NSLog(@"WIN! : %@ ", channel.description); 
      }]; 

並提供了這樣的結果:

2012-12-12 00:17:21.315 Test[13099:c07] (null) 2012-12-12 00:17:21.316 Test[13099:c07] WIN! : GTLYouTubeSearchListResponse 0x727f6d0: {pageInfo:{totalResults,resultsPerPage} etag:""bm4JOKyioI0quSqrxEnI8H7snE4/A2tE7ag9HxUC5HxeD2vDlGNg5iM"" nextPageToken:"CAEQAA" kind:"youtube#searchListResponse" items:[1]}

感謝您的幫助傑夫

+0

很高興聽到你有事情現在的工作!我們確實需要獲得一些「官方」的Objective-C示例代碼。 –

+1

現在有一個適用於YouTube庫的Objective-C示例:https://code.google.com/p/google-api-objectivec-client/source/browse/trunk/Examples/YouTubeSample/YouTubeSampleWindowController.m – grobbins

回答

2

我不是一個Objective-C編程,所以我不能具體如何修改代碼建議,但這是一般問題。

它看起來像你調用YouTube數據API V3 channels.list()方法,它是在https://developers.google.com/youtube/v3/docs/channels/list

的一個必需的參數爲part記錄,並且我認爲這是什麼ueryForChannelsListWithPart()方法,它的一個參數需要英寸part應設置爲類似idid,contentDetails,如文檔中所述。

然後,您需要添加一個附加參數來告訴API您嘗試檢索哪些頻道的信息。各種選項列在文檔的「過濾器」部分。

v3的API不支持在channels.list()調用中指定舊版YouTube用戶名。它確實需要參數id,該參數對應於您要檢索信息的頻道的YouTube頻道ID。這些通常是UC...的形式,並且在RayWilliamJohnson的情況下,通道ID是UCGt7X90Au6BV8rf49BiM6Dg。 (你可以通過撥打電話search.list()得出結論;這是example using the API Explorer。)

請注意,channels.list()方法實際上並不返回視頻列表。如果您想要檢索給定頻道中的最新視頻,則需要撥打channels.list()來獲得contentDetails部分中對應的上傳播放列表ID,然後撥打playlistItems.list()來獲取該上傳播放列表中的視頻。我們有一個在Python中這樣做的例子,但Objective-C中沒有端到端的例子。

+0

i現在就試試看,如果我取得任何成功,更新線程 – ymg

+0

我已經嘗試設置參數,因爲你說過,但沒有爲我工作,我得到相同的錯誤代碼是'-32602' – ymg

1

任何關於如何通過ID查找渠道搜索,使用此

GTLServiceYouTube *youTubeService = [[GTLServiceYouTube alloc] init]; 
youTubeService.APIKey = @"<YOUR_API_KEY>"; 

GTLQueryYouTube *channelsQuery = [GTLQueryYouTube queryForChannelsListWithPart:@"id,snippet,brandingSettings,contentDetails,invideoPromotion,statistics,status,topicDetails"]; 
channelsQuery.identifier = @"<CHANNEL_ID>"; 

[youTubeService executeQuery:channelsQuery completionHandler:^(GTLServiceTicket *ticket, GTLYouTubeChannelListResponse *channelListResponse, NSError *error) { 
    if (error == nil) { 
     if (channelListResponse.items.count > 0) { 
      GTLYouTubeChannel *channel = channelListResponse.items.firstObject; 
      NSLog(@"Got youtube channel - %@", channel); 
     } else { 
      NSLog(@"Cannot find channels with id %@", channelsQuery.identifier); 
     } 
    } else { 
     NSLog(@"Cannot get youtube channels - %@", error); 
    } 
}]; 
+0

我很高興我不必再使用ObjC了,謝謝你的回答,雖然<3 – ymg

+1

也可以用來檢索用戶的頻道 - 只需替換「channelsQuery.identifier = @」「;」與「channelsQuery.mine = YES;」 – AlexeyVMP

+0

Hi @PulKit Goyal,我嘗試與你一樣,但我得到錯誤「沒有篩選器選擇」。你知道爲什麼 –

2
service = [[GTLServiceYouTube alloc] init]; 
service.shouldFetchNextPages = YES; 
service.retryEnabled = YES; 
service.APIKey = @"xxxxxxxx"; 

GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"id,snippet"]; 
query.maxResults = 15; 
query.q = YOUR_SEARCH_TEXT; 
query.fields = @"items(id,snippet)"; 
query.order = @"viewCount"; 

[service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) { 
    if (!error) { 
     for (GTLYouTubeVideoListResponse *videoInfo in object) { 
      [youtubeList addObject:videoInfo.JSON]; 
     } 
     NSLog(@"youtubeList count %lu",(unsigned long)youtubeList.count); 
     NSLog(@"youtubeList %@",youtubeList); 
     [self.tableView reloadData]; 
    } else { 
     NSLog(@"%@", error); 
    } 
    -- i'm test this currently for pagination -- 
    GTLYouTubePlaylistItemListResponse *playlistItems = object; 
    NSLog(@"playlistItems.nextPageToken %@",playlistItems.nextPageToken); 
    NSLog(@"playlistItems.prevPageToken %@",playlistItems.prevPageToken); 
    if(playlistItems.nextPageToken) { 
     NSLog(@"new page!!!"); 
     query.pageToken = playlistItems.nextPageToken; 
     [self requestYoutubeVideo:withText]; 
    } else { 
     NSLog(@"No more pages"); 
    } 
}]; 

`

+0

你可以在[This place](https://console.developers.google.com)得到你的apikey – jose920405

+0

謝謝你的回答,我現在已經轉向Swift了 – ymg

相關問題