使用 scribejava 为 wordpress oauth1.0a 请求访问令牌时出现 Http 连接错误

Http connection error when requesting access token for wordpress oauth1.0a using scribejava

我能够跟随 oauth 舞蹈直到最后一步: 我有一个请求令牌,在用户在浏览器中对应用程序进行身份验证后,oauth 验证程序会发送回我的 Android-app。不幸的是,我无法获得最终访问令牌。这就是我在最后一步中尝试做的事情:

// use the parameter your API exposes for the verifier
String oauthVerifier = uri.getQueryParameter("oauth_verifier");

// get requestToken from shared preferences
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
OAuth1RequestToken requestToken = new OAuth1RequestToken(
        sharedPref.getString("WP_OAuthToken",""),
        // sharedPref.getString("WP_OAuthTokenSecret","")
        oauthVerifier
);

// request accessToken from API
OAuth1AccessToken accessToken = null;
try {
    accessToken = service.getAccessToken(requestToken, oauthVerifier);
} ...

最后一行在这里不起作用。 logcat告诉我

Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getAllByName(InetAddress.java:752)
at com.android.okhttp.internal.Network.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
at com.github.scribejava.core.httpclient.jdk.JDKHttpClient.doExecute(JDKHttpClient.java:104)
at com.github.scribejava.core.httpclient.jdk.JDKHttpClient.execute(JDKHttpClient.java:72)
at com.github.scribejava.core.oauth.OAuthService.execute(OAuthService.java:104)
at com.github.scribejava.core.oauth.OAuth10aService.getAccessToken(OAuth10aService.java:99)
at com.example.******.*********.LoginActivity.onResume(LoginActivity.java:132)

其中 .LoginActivity.onResume(LoginActivity.java:132) 是上面代码的最后一行。在 JDKHttpClient.java 的第 104 行中,它试图在 final HttpURLConnection connection = (HttpURLConnection) new URL(completeUrl).openConnection(); 上 运行 connection.connect();。当我尝试调试错误时,这是​​我的应用程序崩溃的地方。调试输出类似于

this = {JDKHttpClient@4923} 
userAgent = null
headers = {HashMap@4924}  size = 1
 0 = {HashMap$HashMapEntry@4933} "Authorization" -> "OAuth oauth_consumer_key="pxKsS2wdw4uvPPR", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_timestamp="1506338204", oauth_nonce="292945226", oauth_verifier="DrZUpDOsdTN3nOUcP0ZIEsclBYx6", oauth_token="weCu9ejOkmOX9D4zB9zL62XESpe23Q", oauth_signature="%2B%2FV%2FwkmFFaFfmb%2Fc%3D""
httpVerb = {Verb@4837} "POST"
completeUrl = "http://***wordpresspage***.de/oauth1/access"
bodyType = {JDKHttpClient$BodyType@4839} "BYTE_ARRAY"
bodyContents = {byte[0]@4926} 
connection = {HttpURLConnectionImpl@4927} "com.android.okhttp.internal.huc.HttpURLConnectionImpl:http://***wordpresspage***.de/oauth1/access"
 client = {OkHttpClient@4936} 
 fixedContentLength = -1
 followUpCount = 0
 handshake = null
 httpEngine = null
 httpEngineFailure = null
 requestHeaders = {Headers$Builder@4937} 
  namesAndValues = {ArrayList@4942}  size = 6
   0 = "Authorization"
   1 = "OAuth oauth_consumer_key="pxKsS2wdw4uvPPR", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_timestamp="1506338204", oauth_nonce="292945226", oauth_verifier="DrZUpDOsdTN3nOUcP0ZIEsclBYx6", oauth_token="weCu9ejOkmOX9D4zB9zL62XESpe23Q", oauth_signature="%2B%2FV%2FwkmFFaFfmb%2Fc%3D""
   2 = "Content-Length"
   3 = "0"
   4 = "Content-Type"
   5 = "application/x-www-form-urlencoded"
  shadow$_klass_ = {Class@2218} "class com.android.okhttp.Headers$Builder"
  shadow$_monitor_ = -1963988711
 responseHeaders = null
 route = null
 urlFilter = {HttpHandler$CleartextURLFilter@4938} 
 chunkLength = -1
 HttpURLConnection.fixedContentLength = -1
 fixedContentLengthLong = -1
 instanceFollowRedirects = true
 method = "POST"
 responseCode = -1
 responseMessage = null
 allowUserInteraction = false
 connectTimeout = 0
 connected = false
 doInput = true
 doOutput = false
 ifModifiedSince = 0
 readTimeout = 0
 requests = null
 url = {URL@4939} "http://***wordpresspage***.de/oauth1/access"
  authority = "***wordpresspage***.de"
  file = "/oauth1/access"
  handler = {HttpHandler@4951} 
  hashCode = -1
  host = "***wordpresspage***.de"
  hostAddress = null
  path = "/oauth1/access"
  port = -1
  protocol = "http"
  query = null
  ref = null
  userInfo = null
  shadow$_klass_ = {Class@3293} "class java.net.URL"
  shadow$_monitor_ = -2041726017
 useCaches = true
 shadow$_klass_ = {Class@2210} "class com.android.okhttp.internal.huc.HttpURLConnectionImpl"
 shadow$_monitor_ = -1950688533

我只是稍微更改了凭据,因为我不想发布我网站的凭据。

我希望上述连接能够成功,并进一步归还我的 wordpress api 的访问令牌。当我在 Postman 中发送相同的凭据时,请求成功。那么问题出在哪里,当我使用 scribejava 时? 是因为 scribejava 在请求 header 中发送了 oauth_verifier 而在 Postman 中你可以将它作为 url 参数发送吗?

根据日志:

Caused by: android.os.NetworkOnMainThreadException

你在主线程调用服务api方法,尝试使用AsyncTask来处理网络请求。