Youtube 数据 API PlaylistItems:List 呼叫为 iOS 而不是 Android

Youtube Data API PlaylistItems:List call working for iOS but not Android

我正在尝试使用 YouTube 数据 API PlaylistItems: list 将 public 播放列表中的简单 table 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;
        }

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