Youtube上传的视频列表.FileNotFound错误

Kri*_*ant 6 android youtube-api android-youtube-api youtube-data-api

我正在尝试使用以下内容获取在我的YouTube频道上传的视频列表

https://www.googleapis.com/youtube/v3/search?part=snippet&channelId {MY_CHANNEL_ID}&maxResults = 50&key = {MY_APP_ID}

我在Google App Console中创建了应用程序并为其生成了APP ID.但是,当我尝试通过我的Android应用程序访问它时,得到java.io.FileNotFoundException错误

我也给了应用程序标识符和SHA1,如果我尝试通过Web浏览器键访问而没有任何其他约束它运行良好并返回所有视频列表但是在Android的情况下它不起作用.

@Override
    protected String doInBackground(Void... voids) {

        HttpURLConnection urlConnection = null;

        String urlString = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCrF362wkcVnjqqPRsSEzvgg&maxResults=50&key=AIzaSyAiAFjZb1eVdRxVWnymrhuAb1iDlmYupu8";
        //urlString = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=PLTqSMwQKOhUhl7gm7h6YwX6XPYr0ViBtu&key=AIzaSyAiAFjZb1eVdRxVWnymrhuAb1iDlmYupu8";

        String jsonString = new String();

        URL url = null;
        try {
            url = new URL(urlString);

        urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setRequestMethod("GET");
        urlConnection.setReadTimeout(10000 /* milliseconds */);
        urlConnection.setConnectTimeout(15000 /* milliseconds */);

        urlConnection.setDoOutput(true);

        urlConnection.connect();

        BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));

        char[] buffer = new char[1024];

        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line+"\n");
        }
        br.close();

        jsonString = sb.toString();

        System.out.println("JSON: " + jsonString);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return jsonString;
    }
Run Code Online (Sandbox Code Playgroud)

错误日志

BasicNetwork.performRequest: Unexpected response code 403 for https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCrF362wkcVnjqqPRsSEzvgg&maxResults=50&key=AIzaSyAiAFjZb1eVdRxVWnymrhuAb1iDlmYupu8
05-13 15:28:26.607 19118-19151/com.jeevanmulmantra W/System.err: java.io.FileNotFoundException: https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCrF362wkcVnjqqPRsSEzvgg&maxResults=50&key=AIzaSyAiAFjZb1eVdRxVWnymrhuAb1iDlmYupu8
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Wat*_* v2 3

如果不查看您的 API 控制台设置,我无法确定。但从 HTTP 响应来看,您的 IP 地址可能被 Google 授权服务器阻止了。因此,它会向您发送回未经授权的请求 HTTP 状态代码 403。

转至API 控制台,然后从“密钥限制”部分下选择“无”单选按钮。然后再试一次。它应该有效。