2013-06-19 132 views
2

我正嘗試將用戶訂閱到YouTube頻道。因此,用戶登錄到iOS應用,點擊訂閱,然後我將用戶訂閱到預定義的頻道。 所以我有一個應用是在啓用YouTube Data API v3服務的情況下創建的。ios google + api youtube訂閱頻道

範圍:kGTLAuthScopeYouTube

GTLServiceYouTube *service = self.youTubeService; 

GTLYouTubeSubscriptionSnippet* snippet = [GTLYouTubeSubscriptionSnippet object]; 
snippet.channelId = @"UCGRjJrpD2bmk9Ilq6nq80qg"; 

GTLYouTubeSubscription* subscription = [GTLYouTubeSubscription object]; 
subscription.snippet = snippet; 

GTLQueryYouTube *query = [GTLQueryYouTube queryForSubscriptionsInsertWithObject:subscription part:@"contentDetails,snippet"]; 

[service executeQuery:query 
          completionHandler:^(GTLServiceTicket *ticket, 
               GTLYouTubeSubscription *subscription, 
               NSError *error) { 
           /* Callback */ 
           NSLog(@"subscription:%@", subscription); 
           NSLog(@"error:%@", error); 

          }]; 

服務器返回:

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

任何想法,爲什麼會出現這種情況?

我正在使用示例youtube應用程序。

https://developers.google.com/youtube/v3/docs/subscriptions/insert#examples 上的示例url正常工作,從我可以告訴我在objc代碼中做同樣的事情。

POST https://www.googleapis.com/youtube/v3/subscriptions?part=contentDetails%2Csnippet&fields=snippet&key= {} YOUR_API_KEY

內容類型:應用程序/ JSON 授權:承載ya29.AHES6ZQSBniofZhyVX4kfCn0-gVKKeiGayMcQHjTfWxMyffndRus7w X-JavaScript的用戶代理:谷歌API瀏覽器

{ 「片段」 :{ 「RESOURCEID」:{ 「的channelID」: 「UCDPM_n1atn2ijUwHd0NNRQw」 } } }

200個OK

  • 隱藏標題 -

緩存控制:無緩存,無店鋪,最大年齡= 0,必重新驗證 內容編碼:gzip 的Content-Length :286 Content-Type:application/json;字符集= UTF-8 日期:星期五,2013年6月14日11時28分26秒GMT 的Etag: 「2vd4g3cVsHAtTj​​JSdUMaBo1PBVE/Rv5ixqWURoAy7lbp6z3jpkm7IOE」 過期:週五,1990年01一月00:00:00 GMT 雜注:無緩存 服務器: GSE

{ 
"snippet": { 
    "publishedAt": "2013-06-14T11:28:26.000Z", 
    "title": "ColdplayVEVO", 
    "description": "", 
    "resourceId": { 
    "kind": "youtube#channel", 
    "channelId": "UCDPM_n1atn2ijUwHd0NNRQw" 
    }, 
    "channelId": "UCQIKfhQEozSerNr3go189mw", 
    "thumbnails": { 
    "default": { 
    "url": "https://i1.ytimg.com/i/DPM_n1atn2ijUwHd0NNRQw/1.jpg?v=c2f0dd" 
    }, 
    "high": { 
    "url": "https://i1.ytimg.com/i/DPM_n1atn2ijUwHd0NNRQw/mq1.jpg?v=c2f0dd" 
    } 
    } 
} 
} 

更新1個 啓用洛 - 見下文 細節youtube.subscriptions.insert

2013-06-19 08:45:52 +0000 
Request: POST https://www.googleapis.com/rpc?prettyPrint=false 
Request headers: 
    Accept: application/json-rpc 
    Authorization: Bearer _snip_ 
    Cache-Control: no-cache 
    Content-Type: application/json-rpc; charset=utf-8 
    User-Agent: com.boxonline.tpp.bol.think/1.0 google-api-objc-client/2.0 MacOSX/10.8.3 (gzip) 

Request body: (214 bytes) 
{ 
    "jsonrpc" : "2.0", 
    "method" : "youtube.subscriptions.insert", 
    "id" : "gtl_1", 
    "params" : { 
    "fields" : "snippet", 
    "part" : "contentDetails,snippet", 
    "resource" : { 
     "snippet" : { 
     "channelId" : "UCGRjJrpD2bmk9Ilq6nq80qg" 
     } 
    } 
    }, 
    "apiVersion" : "v3" 
} 

Response: status 200 
Response headers: 
    Cache-Control: no-cache, no-store, max-age=0, must-revalidate 
    Content-Encoding: gzip 
    Content-Length: 131 
    Content-Type: application/json; charset=UTF-8 
    Date: Wed, 19 Jun 2013 08:45:52 GMT 
    Expires: Fri, 01 Jan 1990 00:00:00 GMT 
    Pragma: no-cache 
    Server: GSE 
    X-Content-Type-Options: nosniff 
    X-Frame-Options: SAMEORIGIN 
    X-XSS-Protection: 1; mode=block 

Response body: (152 bytes) 
{ 
    "error" : { 
    "message" : "Required", 
    "data" : [ 
     { 
     "reason" : "publisherRequired", 
     "message" : "Required", 
     "domain" : "youtube.subscription" 
     } 
    ], 
    "code" : -32500 
    }, 
    "id" : "gtl_1" 
} 

+0

見http://code.google。com/p/google-api-objectivec-client/wiki /簡介#Logging_HTTP_Server_Traffic – Zsolt

+0

如果你正在做一些快速測試,make xcode會忽略[GTMHTTPFetcher setLoggingEnabled:YES]的警告; #pragma clang diagnostic #pragma clang diagnostic ignored「-Wobjc-protocol-method-implementation」 +(void)setLoggingEnabled:(BOOL)flag { gIsLoggingEnabled = flag; } #pragma clang diagnostic pop – Zsolt

+0

publisherRequired似乎是問題所在。 – Zsolt

回答

2

的搜索解決方案n爲更換:

GTLYouTubeSubscriptionSnippet* snippet = [GTLYouTubeSubscriptionSnippet object]; 
snippet.channelId = @"UCGRjJrpD2bmk9Ilq6nq80qg"; 

與此:

GTLYouTubeSubscriptionSnippet* snippet = [GTLYouTubeSubscriptionSnippet object]; 
GTLYouTubeResourceId* resourceObject = [GTLYouTubeResourceId object]; 
resourceObject.channelId = @"UCGRjJrpD2bmk9Ilq6nq80qg"; 
+0

同樣的問題,但此解決方案也不工作 – returnvoid