0

我尝试为 WoW Requests 编写一个 APP。我是 API 编程新手。首先,我请求我需要请求我的令牌的 authCode。我想了解如何获得此令牌。我读到我需要一个 HTTP 客户端:

“此 CURL 命令将授权代码转换为访问令牌,以代表客户端应用程序和经过身份验证的用户调用 API(需要 HTTP 客户端)”

我认为我误解了令牌请求。

但我得到一个错误:

D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/System.err: android.os.NetworkOnMainThreadException
W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
        at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
W/System.err:     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
        at java.net.InetAddress.getAllByName(InetAddress.java:1152)
        at com.android.okhttp.Dns$1.lookup(Dns.java:41)
        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
        at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144)
        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86)
        at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176)
W/System.err:     at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
        at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
        at de.codeyourapp.battlenet.MainActivity.test(MainActivity.java:111)
        at de.codeyourapp.battlenet.MainActivity.onResume(MainActivity.java:86)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1446)

功能:

public void test(){
        HttpURLConnection connection = null;

        try {
            URL url = new URL ("https://(REGION)battle.net/oauth/token");
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);



               connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
line111!!!!             connection.setRequestProperty  ("Authorization", "Basic " + (CLIENTID+ ":" + SECURE).getBytes("UTF-8"));
           // connection.getOutputStream().write(("&redirect_uri=...&scope=...???&grant_type=authorization_code&code=" + AUTHCODE).getBytes("UTF-8"));


            int rc = connection.getResponseCode();
            System.out.println(rc);


            InputStream content = (InputStream)connection.getInputStream();
            BufferedReader in = new BufferedReader (new InputStreamReader(content));
            String line = in.readLine();

            while(line != null){
                System.out.println(line);
                line = in.readLine();
            }


        }

我的请求必须如下所示:

curl -X POST https://(region).battle.net/oauth/token
-u <developer client id>:<developer secret>
-d redirect_uri=<redirect URI used in authorize request>
-d scope=<space separated scopes>
-d grant_type=authorization_code
-d code=<authorization code>

我只需要令牌来发出请求。有人可以帮我吗?:)

4

0 回答 0