Androidアプリからツイート(固定メッセージ)を送信しようとすると問題が発生します。私は4つの異なるコードサンプル(すべてtwitter4jを使用しています)を使って試しましたが、異なるフォーラムから取りましたが、常に同じ結果が得られました。ユーザーとパスワードを入れて認証がうまくいきましたが、その後は常にリダイレクトされます"callback_url"で指定されたURL(私は既存のURLでテストしたが、既存のURLではテストしていません)に送信します。このツイートは公開されていません。 ご覧のとおり、認証とツイートの送信方法は分かれていますが、認証が完了するとコントロールは決してアプリケーションに返されません。私はアンドロイド2.1(2.2もテスト済み)を使用しています。 本当にありがとうございます。 ありがとうございます。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();
}
AndroidManifestにおける活動の宣言:Androidはアプリがコールバックを処理すべきかを知っているように、
<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>
こんにちは@ jamn224、ご回答いただきありがとうございますが、私はまだこの問題で積み重ねています。私はFrankensteinのコードに従ってAndroid Manifestを修正し、彼が提供したサンプル(AndroidTwitterSample)を統合し、あなたのコードをonResumeに追加しましたが、認証後、私のコードに戻ることはありません。コールバックURL。私のエミュレータで見たエラーメッセージは、 "x-oauthflow-twitterのWebページ:// {MY_CALLBACK_URL}?oauth_token = xxxxxは一時的にダウンしているか、移動している可能性があります..."というエラーメッセージがテストされました。他の提案はありますか? – maxivis
インテントフィルターにすべてのタグを含めましたか? android.intent.category.BROWSABLEとDEFAULTを追加するのを忘れてしまったようです。 – jmcdale
こんにちは@ jamn224、はい、両方のカテゴリをインテントフィルタに追加しました。上記の質問では、TwitterActivity宣言を使ってAndroidManifestを見ることができるので、少なくとも宣言は間違っており、両方のカテゴリは既に含まれています。 – maxivis