我能够跟随oauth舞蹈直到最后一步: 我有一个请求令牌,并且在用户在浏览器中对应用程序进行身份验证后,会将oauth验证程序发送回我的Android应用程序。不幸的是,我无法获得最终的访问令牌。这就是我在最后一步中要做的事情:
// 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$1.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行中,尝试在connection.connect();
上运行final HttpURLConnection connection = (HttpURLConnection) new URL(completeUrl).openConnection();
。当我尝试调试错误时,这就是我的应用程序崩溃的地方。调试输出类似于
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$1@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在请求标头中发送oauth_verifier而在Postman中你可以将它作为url参数发送吗?
答案 0 :(得分:0)
根据日志:
Caused by: android.os.NetworkOnMainThreadException
你在主线程上调用服务api方法,尝试使用AsyncTask来处理网络请求。