Ok, sto creando un client Tumblr per Android, sto provando e non riuscendo a far funzionare OAuth per circa una settimana. Ecco come sta andando:Android Tumblr Oauth-signpost 401
L'utente attiva l'app. Attività principale suCreate fa questo:
settings = getSharedPreferences(PREFS_NAME, 0);
authToken=settings.getString("OauthToken", "none");
authTokenSecret=settings.getString("OauthSecret", "none");
if(authToken=="none" || authTokenSecret=="none"){
Intent i = new Intent(getApplicationContext(),Authentication.class);
startActivity(i);
}
avvia un'attività di autenticazione che contiene una WebView. Tale attività ottiene correttamente il token di richiesta e invia WebView alla schermata di accesso di Tumblr. L'utente è invitato a consentire l'accesso ai propri dati da parte app, premono Consenti e il mio WebViewClient cattura la richiamata Url, e lo fa con esso:
String[] token = helper.getVerifier(url);
if (token != null) {
try {
String accessToken[] = helper.getAccessToken(token[1]);
editor.putString("OauthToken", accessToken[0]);
editor.putString("OauthSecret", accessToken[1]);
editor.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
finish();
getAccessToken del classe helper e getVerifier simile a questa:
public String[] getVerifier(String myUrl) {
// extract the token if it exists
Uri uri = Uri.parse(myUrl);
if (uri == null) {
return null;
}
String token = uri.getQueryParameter("oauth_token");
String verifier = uri.getQueryParameter("oauth_verifier");
return new String[] { token, verifier };
}
public String[] getAccessToken(final String verifier)
throws OAuthMessageSignerException, OAuthNotAuthorizedException,
OAuthExpectationFailedException, OAuthCommunicationException {
new Thread(new Runnable() {
public void run() {
try {
mProvider.retrieveAccessToken(mConsumer, verifier);
} catch (OAuthMessageSignerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthCommunicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
return new String[] {
mConsumer.getToken(), mConsumer.getTokenSecret()
};
}
Poi ho finalmente tornare alla schermata principale dell'applicazione e cerco di fare il mio primo chiamata API, per ottenere i più recenti dieci messaggi su cruscotto dell'utente:
OAuthConsumer myConsumer = new CommonsHttpOAuthConsumer(MainView.authToken, MainView.authTokenSecret);
HttpGet request = new HttpGet("http://api.tumblr.com/v2/user/dashboard?limit=10");
myConsumer.sign(request);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
Tuttavia, invece di ottenere una bella risposta JSON come dovrei essere, sto ottenendo questo:
10-20 16:36:18.110: D/Result(22817): {"meta":{"status":401,"msg":"Not Authorized"},"response":[]}
Allora, dove ho sbagliato? Grazie
Ricevo lo stesso identico problema con il tentativo di utilizzare i metodi API che richiedono richieste POST tuttavia, le richieste GET sembrano funzionare correttamente. Sto iniziando a chiedermi se la loro API è un po 'squilibrata. –
Quale versione di Android stai sviluppando? – NotACleverMan
Solo a nido d'ape. – Nick