2012-05-30 171 views
1

我想通過api訂閱youtube頻道。由於我對xml不太好,所以需要你的幫助。 原文鏈接文檔:https://developers.google.com/youtube/2.0/developers_guide_protocol_subscriptions 或者Youtube訂閱api [錯誤]

總之這裏是寫在YouTube的文檔內容:

添加訂閱

要創建訂閱,您發送標識的YouTube用戶名POST請求用於創建訂閱的已認證用戶。請求的主體是包含以下元素的XML條目:

<category>標記標識用戶正在創建的訂閱的類型。將標記的術語屬性值設置爲用戶,以指示用戶正在訂閱其他用戶的活動(上傳視頻,評分,將視頻標記爲收藏夾等),或將術語屬性值設置爲頻道以指示用戶正在訂閱一個頻道。

<yt:username>標記標識了頻道或其活動正在訂閱的用戶。

訂閱頻道

POST /feeds/api/users/default/subscriptions HTTP/1.1 
Host: gdata.youtube.com 
Content-Type: application/atom+xml 
Content-Length: CONTENT_LENGTH 
Authorization: Bearer ACCESS_TOKEN 
GData-Version: 2 
X-GData-Key: key=DEVELOPER_KEY 

<?xml version="1.0" encoding="UTF-8"?> 
<entry xmlns="http://www.w3.org/2005/Atom" 
    xmlns:yt="http://gdata.youtube.com/schemas/2007"> 
    <category scheme="http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat" 
     term="channel"/> 
    <yt:username>GoogleDevelopers</yt:username> 
</entry> 

我的問題

我如何編寫POST請求?我嘗試了簡單的HTML表單方法後。但它沒有奏效。我得到了以下錯誤。

Content-Type application/x-www-form-urlencoded is not a valid input type. 
+0

任何人都可以幫忙嗎? –

回答

2

由於您使用的是HTML表單提交,它的設置內容類型application/x-www-form-urlencoded但所需的內容類型是application/atom+xml

做你想做什麼,你需要使用JavaScript來發布(爲了簡單,我推薦jQuery)。

var data = //XML Data for post 

$.ajax({ 
    url://Youtube Subscribe Post URL, 
    type:"POST", 
    data:data, 
    Authorization: //ACCESS_TOKEN, 
    GData-Version: '2', 
    X-GData-Key: 'key=' //DEVELOPER_KEY, 
    contentType:"application/atom+xml", 
    dataType:"xml", 
    success: function(){ 
    alert('subscribd'); 
    } 
}); 

我還沒有測試過,但你應該明白了。確保將評論替換爲應用的實際值。

請注意,這可能會遇到跨域問題。