2011-12-03 67 views
1

我在尝试从我的Android应用发送推文(固定消息)时遇到问题。我用4个不同的代码示例(全部使用twitter4j)从不同的论坛中尝试过,但总是得到相同的结果:我把我的用户和密码,认证工作正常,但之后,我总是重定向到“callback_url”中指定的URL(我已经使用现有的URL进行了测试,并且不存在),并且不发布推文。 正如你所看到的,我已经分离了用于认证和发送推文的方法,但是一旦认证完成,控件就永远不会返回到我的应用程序。我使用的是android 2.1(也是使用2.2进行测试)。 任何帮助将非常感激。 在此先感谢。在AndroidManifest活动无法使用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(); 
} 

声明:

<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> 

回答

0

在Android应用的方案和主机应该是唯一的应用程序,使机器人知道什么应用程序应该处理的回调。你现在拥有的东西让android认为你的应用可以使用“http”方案处理网页浏览。 Check out the answers to the post here.具体来说,frankenstein描述了如何设置方案和主机,我的代码将告诉你如何处理onResume()。

+0

嗨@ jamn224,感谢您的回复,但我仍然在这个问题上堆积如山。根据Frankenstein的代码,我已经修复了Android Manifest,同时也整合了他提供的示例(AndroidTwitterSample),将您的代码添加到onResume中,但是在验证之后,我再也不会回到我的代码中,应用程序总是尝试将我重定向到回调网址。在测试完更改后,我在模拟器中看到的错误消息是“x-oauthflow-twitter:// {MY_CALLBACK_URL}?oauth_token = xxxxx上的网页可能暂时关闭,或者它可能已移动......”。你有其他建议吗? – maxivis

+0

您是否在意向过滤器中包含了所有标签?好像你可能忘记添加android.intent.category.BROWSABLE和DEFAULT。 – jmcdale

+0

Hi @ jamn224,是的,我将两个类别都添加到意图过滤器。在上面的问题中,您可以看到带有TwitterActivity声明的AndroidManifest,因此至少声明是错误的,这两个类都已包含在内。 – maxivis

0

Here是一个文件解决方案的链接,它解释了如何将图片/文本推文整合到android应用中。

我刚刚通过处理这个问题,而不是尝试使用提供的代码尝试修复您的代码。它应该没有修改地工作,并可以通过一个tweet()函数的一个调用发送推文。

+0

非常感谢@MindSpiker,我会检查它 – maxivis