Java 战网API Android Studio获取令牌?
我试着为魔兽世界的请求编写一个应用程序。我是API编程新手。 首先,我请求authCode,我需要它来请求我的令牌。 我想知道我怎样才能得到这个代币。我了解到我需要一个HTTP客户端: “此CURL命令将授权代码转换为访问令牌,用于代表客户端应用程序和经过身份验证的用户调用API(需要HTTP客户端)” 我想我误解了代币请求 但我有一个错误:Java 战网API Android Studio获取令牌?,java,android,api,oauth-2.0,Java,Android,Api,Oauth 2.0,我试着为魔兽世界的请求编写一个应用程序。我是API编程新手。 首先,我请求authCode,我需要它来请求我的令牌。 我想知道我怎样才能得到这个代币。我了解到我需要一个HTTP客户端: “此CURL命令将授权代码转换为访问令牌,用于代表客户端应用程序和经过身份验证的用户调用API(需要HTTP客户端)” 我想我误解了代币请求 但我有一个错误: D/NetworkSecurityConfig: No Network Security Config specified, using platform
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>
curl-X POST https://(地区)。battle.net/oauth/token
-u:
-d_uri=
-d范围=
-d授权类型=授权代码
-d代码=
我只需要令牌就可以发出请求。有人能帮我吗?:) 您正在UI线程上执行导致问题的API请求。这样检查一下posthttps://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexceptionthank 谢谢你的回复:)我会试试的