2012-10-24 47 views
0

我正嘗試使用Youtube GDATA API將新播放列表添加到YouTube帳戶。 我將我的代碼基於文檔:https://developers.google.com/youtube/2.0/developers_guide_protocol_playlists#Adding_a_playlist使用Youtube DATA API在Android上創建播放列表

我首先得到一個訪問令牌並適當地使用我的開發人員密鑰。 該帖子似乎工作得很好,但是當試圖找回響應時,在調用getInputStream時出現文件未找到異常。

有沒有人有想法? 感謝

這裏是連接代碼(更新版清潔劑):

@Override 
protected Boolean doInBackground(Void... arg0) { 
    BufferedReader input = null; 
    InputStreamReader inputStreamReader = null; 
    StringBuilder postContentXml = new StringBuilder(); 

    postContentXml.append("<?xml version='1.0' encoding='UTF-8'?>"). 
     append("<entry xmlns='http://www.w3.org/2005/Atom'") . 
     append(" xmlns:yt='http://gdata.youtube.com/schemas/2007'>"). 
     append("<title type='text'>Sports Highlights Playlist</title>"). 
     append("<summary>A selection of sports highlights</summary>"). 
     append("</entry>"); 

    byte[] buffer = postContentXml.toString().getBytes(); 
    StringBuilder response = new StringBuilder(); 

    try { 
     URL url = new URL("https://gdata.youtube.com/feeds/api/users/default/playlists"); 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

     // Initialize connection parameters 
     urlConnection.setConnectTimeout(30000); 
     urlConnection.setReadTimeout(30000); 
     urlConnection.setDoOutput(true); 
     urlConnection.setRequestMethod("POST"); 

     // Headers initialization 
     urlConnection.setRequestProperty("Content-Type", "application/atom+xml"); 
     urlConnection.setRequestProperty("Content-Length", String.valueOf(buffer.length)); 
     urlConnection.setRequestProperty("Authorization", mAuthToken); 
     urlConnection.setRequestProperty("GData-Version", "2"); 
     urlConnection.setRequestProperty("X-GData-Key", YoutubeUtils.getDevKey()); 

     OutputStream os = urlConnection.getOutputStream(); 
     os.write(buffer, 0, buffer.length); 
     os.flush(); 
     os.close(); 

     InputStream inputStream = urlConnection.getInputStream(); 

     inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); 


     input = new BufferedReader(inputStreamReader, 4096); 

     String strLine = null; 
     while ((strLine = input.readLine()) != null) { 
      response.append(strLine); 
     } 

     input.close(); 
     inputStreamReader.close(); 
     inputStream.close(); 
     urlConnection.disconnect(); 

     Log.d("CreatePlaylistTask", "Response: " + response); 
    } 
    catch(Exception e) { 
     Log.d("CreatePlaylistTask", "Error occured: " + e.getMessage()); 
    } 

    return true; 
} 

回答

2

我假設POST本來就不是成功的。

如果我不得不通過查看代碼來猜測,我認爲問題可能是Authorization標題值。 myAuthToken是什麼樣的,它是什麼類型的令牌?例如,如果它是一個OAuth 2令牌,則該值需要爲Bearer TOKEN_VALUE,而不僅僅是TOKEN_VALUE

此外,請注意,v3 of the YouTube Data API將在不久的將來發布,它將使用新的Google APIs Client Library for Java在Android上提供更好的支持。

+0

而當然,你是對的:) 謝謝傑夫的快速反應。 POST失敗,出現403響應。我沒有注意到這個API可能只返回一個狀態結果,而不是一個完整的XML或JSON響應。 我使用Android的AccountManager getAuthToken函數獲取令牌,包括重新驗證令牌,但沒有成功。我已經包括持票人,但沒有奏效。 如您所述,最終,我確實遇到了Google API客戶端庫,其中包括最新3月份的一個很棒的Google IO視頻。 我會盡量使用它。 – asaf

+0

順便說一句,如果你已經提到了數據API的近期發佈,請詳細說明6月份推出的Android本地嵌入API的狀態。我似乎無法在網絡上的任何地方找到任何時間框架參考。 我不想使用非法的直接流式嵌入,並且使用IFrame嵌入式API有許多缺陷和錯誤:( – asaf

+0

Data API將很快公開發布。本機Android YouTube Player API需要更長時間被釋放,但不是一個非常長的時間段。(對不起,我不能得到更具體的。) –

1

我已經整理了一個示例Android應用程序,它使用YouTube Data v3 API來演示如何將播放列表加載到ListView中。

https://github.com/akoscz/YouTubePlaylist

請注意,您MUST有此示例應用程序的工作有效的API密鑰。您可以使用Google Developer Console註冊您的應用程序並啓用YouTube數據API。您需要註冊一個Web應用程序而非Android應用程序,因爲此示例應用程序使用的API密鑰是「瀏覽器密鑰」。

相關問題