1

我正在尝试使用 OAuth2.0 在 youtube API 上进行身份验证(就像可以在文档中看到的那样)但由于某种原因我无法获得它......

这是我的连接代码:

        URL url = new URL("https://accounts.google.com/o/oauth2/device/code");
        URLConnection conn = url.openConnection();

        conn.setDoOutput(true);
        conn.setRequestProperty("Host", "accounts.google.com");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write("client_id=999999999999999.apps.googleusercontent.com&scope=https://gdata.youtube.com");
        wr.flush();

        // Get the response
        StringBuilder sb = new StringBuilder();
        InputStreamReader is = new InputStreamReader(conn.getInputStream());
        BufferedReader rd = new BufferedReader(is);
        String line = "";
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        wr.close();
        rd.close();
        line = sb.toString();

这是我得到的错误:

        07-14 18:03:32.320: W/System.err(7902): java.net.UnknownHostException: Unable to resolve host "accounts.google.com": No address associated with hostname
        07-14 18:03:32.330: W/System.err(7902):     at java.net.InetAddress.lookupHostByName(InetAddress.java:400)
        07-14 18:03:32.335: W/System.err(7902):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
        07-14 18:03:32.335: W/System.err(7902):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
        07-14 18:03:32.335: W/System.err(7902):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
        07-14 18:03:32.335: W/System.err(7902):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
        07-14 18:03:32.335: W/System.err(7902):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
        07-14 18:03:32.340: W/System.err(7902):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
        07-14 18:03:32.340: W/System.err(7902):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
        07-14 18:03:32.340: W/System.err(7902):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
        07-14 18:03:32.340: W/System.err(7902):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
        07-14 18:03:32.340: W/System.err(7902):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:441)
        07-14 18:03:32.345: W/System.err(7902):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
        07-14 18:03:32.345: W/System.err(7902):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
        07-14 18:03:32.345: W/System.err(7902):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
        07-14 18:03:32.350: W/System.err(7902):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
        07-14 18:03:32.350: W/System.err(7902):     at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:280)
        07-14 18:03:32.350: W/System.err(7902):     at pt.dporem.your.subscriptions.sychronization.OAuth.doInBackground(OAuth.java:58)
        07-14 18:03:32.355: W/System.err(7902):     at pt.dporem.your.subscriptions.sychronization.OAuth.doInBackground(OAuth.java:1)
        07-14 18:03:32.355: W/System.err(7902):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
        07-14 18:03:32.355: W/System.err(7902):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        07-14 18:03:32.355: W/System.err(7902):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        07-14 18:03:32.360: W/System.err(7902):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
        07-14 18:03:32.360: W/System.err(7902):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        07-14 18:03:32.360: W/System.err(7902):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        07-14 18:03:32.360: W/System.err(7902):     at java.lang.Thread.run(Thread.java:856)

我究竟做错了什么?谢谢你的帮助。

4

0 回答 0