I was able to follow the oauth dance up to the very last step: I have a request token and an oauth verifier is send back to my Android-app after the user authenticated the app in the browser. Unfortunately I am not able to acquire the final access token. This is what I am trying to do in this very last step:
// 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);
} ...
The very last line is not working here. logcat tells me
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)
where .LoginActivity.onResume(LoginActivity.java:132)
is the last line in above code. In line 104 of the JDKHttpClient.java it is tried to run connection.connect();
on final HttpURLConnection connection = (HttpURLConnection) new URL(completeUrl).openConnection();
. This is the point where my app crashes, when I try to debug the error. The debug output is something like
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
I only slightly changed the credentials, since I don't want credentials to my site being published.
I would expect the above connection to be successful and furthermore to give back the access token for my wordpress api. When I send the same credentials in Postman, the request is successful. So where is the problem, when I use scribejava? Is it because scribejava sends the oauth_verifier in the request header and in Postman you can send it as url parameter?
According to the logs :
Caused by: android.os.NetworkOnMainThreadException
you call the service api methods on the main thread, try to use AsyncTask to handle network requests.