【问题标题】:Youtube Data API PlaylistItems:List call working for iOS but not AndroidYoutube Data API PlaylistItems:列表调用适用于 iOS 但不适用于 Android
【发布时间】:2016-05-27 17:14:59
【问题描述】:

我正在尝试使用 YouTube Data API PlaylistItems: list 调用将一个简单的 youtube 视频表格从公共播放列表添加到我现有的 Android 应用程序中。

我已经成功地让它在我的应用程序的 iOS 版本上运行,但它不适用于我的应用程序的 Android 版本。我不想(也不应该)使用 OAuth,而只需要使用我生成的 API 密钥。我尝试过使用 Android API 密钥和浏览器 API 密钥,但它们都不起作用。

我不断收到此错误消息:

"error": {  
    "errors": [   {    
        "domain": "global",    
        "reason": "required",    
        "message": "Login Required",    
        "locationType": "header",    
        "location": "Authorization"   }  ],  
    "code": 401,  
    "message": "Login Required" }}

这是我的 URL:https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=MY_PLAYLIST_ID&key=MY_API_KEY 这个 URL 适用于 iOS 版本,当我在浏览器中键入它时,这里也是:https://developers.google.com/youtube/v3/docs/playlistItems/list#examples,所有这些都没有使用任何类型的身份验证。我试过更新我的 API 密钥,但也没有用。这是我的代码,以防万一:

@Override
        protected String doInBackground(Void... params) {
            try {
                URL urlConnection = new URL(url_from_above);
                HttpURLConnection connection = (HttpURLConnection) urlConnection
                        .openConnection();
                connection.setReadTimeout(15000);
                connection.setConnectTimeout(15000);
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setRequestMethod("GET");

                int responseCode = connection.getResponseCode();

                if (responseCode == HttpsURLConnection.HTTP_OK) {
                    String line;
                    String jsonString = "";
                    BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    while ((line=br.readLine()) != null) {
                        jsonString+=line;
                    }
                    result = jsonString;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }

【问题讨论】:

    标签: android youtube-data-api


    【解决方案1】:

    所以经过更长时间的试验和错误后,我发现问题出在connection.setDoOutput(true); 在对为什么会破坏它进行一些简短研究后,我发现:What exactly does URLConnection.setDoOutput() affect?

    【讨论】:

      猜你喜欢
      • 2016-07-14
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-07
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多