2017-09-25 1 views
0

Ich konnte dem oauth Tanz bis zum allerletzten Schritt folgen: Ich habe ein Anforderungs-Token und ein oauth Verifier wird zurück zu meiner Android-App gesendet nachdem der Benutzer die App im Browser authentifiziert hat. Leider bin ich nicht in der Lage, den endgültigen Zugriffstoken zu erwerben. Dies ist, was ich versuche, in diesem allerletzten Schritt zu tun:Http Verbindungsfehler beim Anfordern von Zugriffstoken für WordPress oauth1.0a mit scribejava

// 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); 
} ... 

Die letzte Zeile funktioniert hier nicht. logcat sagt mir

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) 

wo .LoginActivity.onResume(LoginActivity.java:132) die letzte Zeile in obigem Code ist. In Zeile 104 des JDKHttpClient.java wird versucht, connection.connect(); unter final HttpURLConnection connection = (HttpURLConnection) new URL(completeUrl).openConnection(); auszuführen. Dies ist der Punkt, an dem meine App abstürzt, wenn ich versuche, den Fehler zu debuggen. Die Debug-Ausgabe ist so etwas wie

this = {[email protected]} 
userAgent = null 
headers = {[email protected]} size = 1 
0 = {[email protected]} "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 = {[email protected]} "POST" 
completeUrl = "http://***wordpresspage***.de/oauth1/access" 
bodyType = {[email protected]} "BYTE_ARRAY" 
bodyContents = {byte[0]@4926} 
connection = {[email protected]} "com.android.okhttp.internal.huc.HttpURLConnectionImpl:http://***wordpresspage***.de/oauth1/access" 
client = {[email protected]} 
fixedContentLength = -1 
followUpCount = 0 
handshake = null 
httpEngine = null 
httpEngineFailure = null 
requestHeaders = {[email protected]} 
    namesAndValues = {[email protected]} 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_ = {[email protected]} "class com.android.okhttp.Headers$Builder" 
    shadow$_monitor_ = -1963988711 
responseHeaders = null 
route = null 
urlFilter = {[email protected]} 
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 = {[email protected]} "http://***wordpresspage***.de/oauth1/access" 
    authority = "***wordpresspage***.de" 
    file = "/oauth1/access" 
    handler = {[email protected]} 
    hashCode = -1 
    host = "***wordpresspage***.de" 
    hostAddress = null 
    path = "/oauth1/access" 
    port = -1 
    protocol = "http" 
    query = null 
    ref = null 
    userInfo = null 
    shadow$_klass_ = {[email protected]} "class java.net.URL" 
    shadow$_monitor_ = -2041726017 
useCaches = true 
shadow$_klass_ = {[email protected]} "class com.android.okhttp.internal.huc.HttpURLConnectionImpl" 
shadow$_monitor_ = -1950688533 

ich nur leicht die Anmeldeinformationen geändert, da ich Anmeldeinformationen auf meiner Website nicht will, veröffentlicht.

Ich würde erwarten, dass die oben genannte Verbindung erfolgreich ist und außerdem das Zugriffstoken für meine Wordpress API zurückgeben. Wenn ich dieselben Anmeldeinformationen in Postman sende, ist die Anfrage erfolgreich. Wo ist das Problem, wenn ich scribejava benutze? Liegt es daran, dass Scribejava den oauth_verifier im Anfrage-Header sendet und in Postman als URL-Parameter?

Antwort

0

Nach den Protokollen:

Caused by: android.os.NetworkOnMainThreadException 

Sie die Service-API-Methoden auf dem Haupt-Thread aufrufen, versuchen AsyncTask verwenden Netzwerkanforderungen zu behandeln.

+0

Gosh .. Vielen Dank für die schnelle Antwort! Jetzt funktioniert es .. –

Verwandte Themen