Android 应用程序的 Twitter 集成

发布于 2024-10-10 06:05:17 字数 2614 浏览 6 评论 0原文

我需要将 Twitter 与我的 Android 应用程序集成,这是我的代码(基于 this):

package com.anta40.oauthsetup;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
import twitter4j.Status;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.util.List;

public class OAuthSetup extends Activity {
Twitter twitter;
RequestToken requestToken;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String CONSUMER_KEY = "xxxxxxxxxx";
    String CONSUMER_SECRET = "yyyyyyyyyyyyyyyyyyyy";
    
    try {
        twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        RequestToken requestToken = twitter.getOAuthRequestToken();
        AccessToken accessToken = null;
        String url = requestToken.getAuthorizationURL();
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
    }
    catch (TwitterException e){
        e.printStackTrace();
    }
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Uri uri = intent.getData();
    
    try {
        String verifier = uri.getQueryParameter("oauth_verifier");
        AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
        String token = accessToken.getToken();
        String secret = accessToken.getTokenSecret();
        displayTimeLine(token, secret);
    }
    catch (TwitterException e){
        e.printStackTrace();
    }
}

@SuppressWarnings("deprecation")
void displayTimeLine(String token, String secret) {
    if (null != token && null != secret) {
        List<Status> statuses = null;
        try {
            twitter.setOAuthAccessToken(token, secret);
            statuses = twitter.getFriendsTimeline();
            Toast.makeText(this, statuses.get(0).getText(), Toast.LENGTH_LONG)
                .show();
        } catch (Exception ex) {
            Toast.makeText(this, "Error:" + ex.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.d("Main.displayTimeline",""+ex.getMessage());
        }
        
    } else {
        Toast.makeText(this, "Not Verified", Toast.LENGTH_LONG).show();
    }
}

}

现在,我如何使用 Dialog 来输入 Twitter 用户名和密码,而不是使用 WebView?密码?

I need to integrate Twitter with my Android app, and this is my code (based on this):

package com.anta40.oauthsetup;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
import twitter4j.Status;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.util.List;

public class OAuthSetup extends Activity {
Twitter twitter;
RequestToken requestToken;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String CONSUMER_KEY = "xxxxxxxxxx";
    String CONSUMER_SECRET = "yyyyyyyyyyyyyyyyyyyy";
    
    try {
        twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        RequestToken requestToken = twitter.getOAuthRequestToken();
        AccessToken accessToken = null;
        String url = requestToken.getAuthorizationURL();
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
    }
    catch (TwitterException e){
        e.printStackTrace();
    }
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Uri uri = intent.getData();
    
    try {
        String verifier = uri.getQueryParameter("oauth_verifier");
        AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
        String token = accessToken.getToken();
        String secret = accessToken.getTokenSecret();
        displayTimeLine(token, secret);
    }
    catch (TwitterException e){
        e.printStackTrace();
    }
}

@SuppressWarnings("deprecation")
void displayTimeLine(String token, String secret) {
    if (null != token && null != secret) {
        List<Status> statuses = null;
        try {
            twitter.setOAuthAccessToken(token, secret);
            statuses = twitter.getFriendsTimeline();
            Toast.makeText(this, statuses.get(0).getText(), Toast.LENGTH_LONG)
                .show();
        } catch (Exception ex) {
            Toast.makeText(this, "Error:" + ex.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.d("Main.displayTimeline",""+ex.getMessage());
        }
        
    } else {
        Toast.makeText(this, "Not Verified", Toast.LENGTH_LONG).show();
    }
}

}

Now, instead of using WebView, how can I use Dialog to input the Twitter username & password?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺孤伤于心 2024-10-17 06:05:17

你不能。 Twitter 现在只允许第三方应用程序使用誓言对其自身进行身份验证。

好吧,让我澄清一下,你可以放置一个对话框并让用户在其中输入用户名/密码,但 Twitter 不再为其提供任何 api。

您可以在此处找到有关该主题的更多信息:http://dev.twitter.com/pages/oauth_faq

You can't. Twitter will only let 3rd party apps use oath to authenticate with itself now.

Well, let me clarify that, you can put a dialog box and let the user enter there username/password, but twitter doesn't supply any api's for it anymore.

You can find more information on the topic here: http://dev.twitter.com/pages/oauth_faq

抠脚大汉 2024-10-17 06:05:17

https://dev.twitter.com/docs/oauth/xauth - 看看这个功能,也许对你有帮助

https://dev.twitter.com/docs/oauth/xauth - look at this feature, maybe it will helps you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文