Je suis confronté à un problème lors d'une tentative d'envoi d'un tweet (message fixe) à partir de mon application Android. Je l'ai essayé avec 4 échantillons de code différents (tous en utilisant twitter4j), pris de différents forums, mais toujours eu le même résultat: je mets mon utilisateur et mot de passe, l'authentification fonctionne bien, mais après je suis toujours redirigé à l'URL spécifiée dans "callback_url" (j'ai testé avec des URLs existantes et non existantes) et le tweet n'est pas publié. Comme vous pouvez le voir, j'ai séparé les méthodes d'authentification et d'envoi du tweet, mais une fois l'authentification terminée, le contrôle n'est jamais renvoyé à mon application. J'utilise Android 2.1 (testé avec 2.2 aussi). Toute aide sera vraiment appréciée. Merci d'avance.Je ne peux pas tweeter avec twitter4j
public void onCreate(Bundle savedInstanceState) {
mTwitter = new TwitterFactory().getInstance();
mTwitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
if (mPrefs.contains(PREF_ACCESS_TOKEN)) {
loginAuthorisedUser();
} else {
loginNewUser();
}
}
private void loginNewUser() {
try {
mReqToken = mTwitter.getOAuthRequestToken(CALLBACK_URL);
WebView twitterSite = new WebView(this);
twitterSite.loadUrl(mReqToken.getAuthenticationURL());
setContentView(twitterSite);
} catch (TwitterException e) {
Toast.makeText(this, "Twitter Login error, try again later", Toast.LENGTH_SHORT).show();
}
}
private void loginAuthorisedUser() {
String token = mPrefs.getString(PREF_ACCESS_TOKEN, null);
String secret = mPrefs.getString(PREF_ACCESS_TOKEN_SECRET, null);
// Create the twitter access token from the credentials we got previously
AccessToken at = new AccessToken(token, secret);
mTwitter.setOAuthAccessToken(at);
Toast.makeText(this, "Welcome back", Toast.LENGTH_SHORT).show();
buttonTweet(null);
}
/**
* Catch when Twitter redirects back to our {@link CALLBACK_URL}</br>
* We use onNewIntent as in our manifest we have singleInstance="true" if we did not the
* getOAuthAccessToken() call would fail
*/
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
dealWithTwitterResponse(intent);
}
/**
* Twitter has sent us back into our app</br>
* Within the intent it set back we have a 'key' we can use to authenticate the user
*
* @param intent
*/
private void dealWithTwitterResponse(Intent intent) {
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) { // If the user has just logged in
String oauthVerifier = uri.getQueryParameter("oauth_verifier");
authoriseNewUser(oauthVerifier);
}
}
/**
* Create an access token for this new user</br>
* Fill out the Twitter4j helper</br>
* And save these credentials so we can log the user straight in next time
*
* @param oauthVerifier
*/
private void authoriseNewUser(String oauthVerifier) {
try {
AccessToken at = mTwitter.getOAuthAccessToken(mReqToken, oauthVerifier);
mTwitter.setOAuthAccessToken(at);
saveAccessToken(at);
// Set the content view back after we changed to a webview
// setContentView(R.layout.main);
tweetMessage();
} catch (TwitterException e) {
Toast.makeText(this, "Twitter auth error x01, try again later", Toast.LENGTH_SHORT).show();
}
}
private void tweetMessage() {
try {
mTwitter.updateStatus("Test - Tweeting");
Toast.makeText(this, "Tweet Successful!", Toast.LENGTH_SHORT).show();
} catch (TwitterException e) {
Toast.makeText(this, "Tweet error, try again later", Toast.LENGTH_SHORT).show();
}
}
private void saveAccessToken(AccessToken at) {
String token = at.getToken();
String secret = at.getTokenSecret();
Editor editor = mPrefs.edit();
editor.putString(PREF_ACCESS_TOKEN, token);
editor.putString(PREF_ACCESS_TOKEN_SECRET, secret);
editor.commit();
}
Déclaration d'activité AndroidManifest:
<activity android:name=".share.twitter.TwitterActivity" android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http://www.someurl.com" />
</intent-filter>
</activity>
Salut @ jamn224, merci pour votre réponse, mais je suis toujours empilé avec ce problème. J'ai corrigé le Manifeste Android selon le code de Frankenstein, j'ai également intégré l'échantillon qu'il a fourni (AndroidTwitterSample), ajouté votre code dans onResume, mais après authentification, je ne reviens jamais à mon code, l'application essaie toujours de me rediriger vers URL de rappel. Après avoir testé les modifications, le message d'erreur que j'ai vu dans mon émulateur était "La page Web de x-oauthflow-twitter: // {MY_CALLBACK_URL}? Oauth_token = xxxxx peut être temporairement arrêtée ou peut-être déplacée ...". Avez-vous d'autres suggestions? – maxivis
Avez-vous inclus toutes les balises dans le filtre d'intention? Il semble que vous ayez oublié d'ajouter android.intent.category.BROWSABLE et DEFAULT. – jmcdale
Salut @ jamn224, oui, j'ai ajouté les deux catégories au filtre d'intention. Dans la question ci-dessus, vous pouvez voir le AndroidManifest avec la déclaration TwitterActivity, donc au moins que la déclaration est erronée, les deux catégories sont déjà incluses. – maxivis