我正嘗試使用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;
}
而當然,你是對的:) 謝謝傑夫的快速反應。 POST失敗,出現403響應。我沒有注意到這個API可能只返回一個狀態結果,而不是一個完整的XML或JSON響應。 我使用Android的AccountManager getAuthToken函數獲取令牌,包括重新驗證令牌,但沒有成功。我已經包括持票人,但沒有奏效。 如您所述,最終,我確實遇到了Google API客戶端庫,其中包括最新3月份的一個很棒的Google IO視頻。 我會盡量使用它。 – asaf
順便說一句,如果你已經提到了數據API的近期發佈,請詳細說明6月份推出的Android本地嵌入API的狀態。我似乎無法在網絡上的任何地方找到任何時間框架參考。 我不想使用非法的直接流式嵌入,並且使用IFrame嵌入式API有許多缺陷和錯誤:( – asaf
Data API將很快公開發布。本機Android YouTube Player API需要更長時間被釋放,但不是一個非常長的時間段。(對不起,我不能得到更具體的。) –