0

我在我的应用程序中使用了一个共享按钮,它使用 twitter 如下:

 public class TwitterApp {
        private Twitter mTwitter;
        private TwitterSession mSession;
        private AccessToken mAccessToken;
        private CommonsHttpOAuthConsumer mHttpOauthConsumer;
        private OAuthProvider mHttpOauthprovider;
        private String mConsumerKey;
        private String mSecretKey;
        private ProgressDialog mProgressDlg;
        private TwDialogListener mListener;
        private Context context;

        public static final String CALLBACK_URL = "http://www.rommanapps.com";
        private static final String TAG = "TwitterApp";

        public TwitterApp(Context context, String consumerKey, String secretKey) {
            this.context    = context;

            mTwitter        = new TwitterFactory().getInstance();
            mSession        = new TwitterSession(context);
            mProgressDlg    = new ProgressDialog(context);

            mProgressDlg.requestWindowFeature(Window.FEATURE_NO_TITLE);

            mConsumerKey    = consumerKey;
            mSecretKey      = secretKey;
                mTwitter.setOAuthConsumer(consumerKey, secretKey);

            mHttpOauthConsumer = new CommonsHttpOAuthConsumer(mConsumerKey, mSecretKey);
            mHttpOauthprovider = new DefaultOAuthProvider("https://api.twitter.com/oauth/request_token",
                                                         "https://api.twitter.com/oauth/access_token",
                                                         "https://api.twitter.com/oauth/authorize");

            mAccessToken    = mSession.getAccessToken();

            configureToken();
        }

        public void setListener(TwDialogListener listener) {
            mListener = listener;
        }

        @SuppressWarnings("deprecation")
        private void configureToken() {
            if (mAccessToken != null) {
                mTwitter.setOAuthConsumer(mConsumerKey, mSecretKey);


                    mTwitter.setOAuthAccessToken(mAccessToken);

            }
        }

        public boolean hasAccessToken() {
            return (mAccessToken == null) ? false : true;
        }

        public void resetAccessToken() {
            if (mAccessToken != null) {
                mSession.resetAccessToken();

                mAccessToken = null;
            }
        }

        public String getUsername() {
            return mSession.getUsername();
        }

        public void updateStatus(String status) throws Exception {
            try {
                mTwitter.updateStatus(status);
            } catch (TwitterException e) {
                throw e;
            }
        }

        public void authorize() {
            mProgressDlg.setMessage("Initializing ...");
            mProgressDlg.show();

            new Thread() {
                @Override
                public void run() {
                    String authUrl = "";
                    int what = 1;

                    try {
                        authUrl = mHttpOauthprovider.retrieveRequestToken(mHttpOauthConsumer, CALLBACK_URL);    

                        what = 0;

                        Log.d(TAG, "Request token url " + authUrl);
                    } catch (Exception e) {
                        Log.d(TAG, "Failed to get request token");

                        e.printStackTrace();
                    }

                    mHandler.sendMessage(mHandler.obtainMessage(what, 1, 0, authUrl));
                }
            }.start();
        }

        public void processToken(String callbackUrl)  {
            mProgressDlg.setMessage("Finalizing ...");
            mProgressDlg.show();

            final String verifier = getVerifier(callbackUrl);

            new Thread() {
                @Override
                public void run() {
                    int what = 1;

                    try {
                        mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer, verifier);

                        mAccessToken = new AccessToken(mHttpOauthConsumer.getToken(), mHttpOauthConsumer.getTokenSecret());

                        configureToken();

                        User user = mTwitter.verifyCredentials();

                        mSession.storeAccessToken(mAccessToken, user.getName());

                        what = 0;
                    } catch (Exception e){
                        Log.d(TAG, "Error getting access token");

                        e.printStackTrace();
                    }

                    mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0));
                }
            }.start();
        }

        private String getVerifier(String callbackUrl) {
            String verifier  = "";

            try {
                callbackUrl = callbackUrl.replace("twitterapp", "http");

                URL url         = new URL(callbackUrl);
                String query    = url.getQuery();

                String array[]  = query.split("&");

                for (String parameter : array) {
                     String v[] = parameter.split("=");

                     if (URLDecoder.decode(v[0]).equals(oauth.signpost.OAuth.OAUTH_VERIFIER)) {
                         verifier = URLDecoder.decode(v[1]);
                         break;
                     }
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            return verifier;
        }

        private void showLoginDialog(String url) {
            final TwDialogListener listener = new TwDialogListener() {
                @Override
                public void onComplete(String value) {
                    processToken(value);
                }

                @Override
                public void onError(String value) {
                    mListener.onError("Failed opening authorization page");
                }
            };

            new TwitterDialog(context, url, listener).show();
        }

        private Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                mProgressDlg.dismiss();

                if (msg.what == 1) {
                    if (msg.arg1 == 1)
                        mListener.onError("Error getting request token");
                    else
                        mListener.onError("Error getting access token");
                } else {
                    if (msg.arg1 == 1)
                        showLoginDialog((String) msg.obj);
                    else
                        mListener.onComplete("");
                }
            }
        };

        public interface TwDialogListener {
            public void onComplete(String value);       

            public void onError(String value);
        }
    }

我对这个陈述有误

  1. mHttpOauthprovider = new DefaultOAuthProvider("https://api.twitter.com/oauth/request_token", "https://api.twitter.com/oauth/access_token", "https://api.twitter.com/oauth/授权”);

它说构造函数 DefaultOAuthProvider(String, String, String) 未定义

  1. mAccessToken = mSession.getAccessToken();

上面写着Type mismatch: cannot convert from twitter4j.auth.AccessToken to twitter4j.http.AccessToken

而且我不知道问题出在哪里!请帮忙

4

1 回答 1

0

我使用了以下链接中提供的库。它可以帮助我轻松地将 twitter 与 facebook 和 linkedin 集成到我的 android 应用程序中。

该项目还表示,他们希望尽快增加更多的社交客户。

http://socialauth.in/

于 2012-12-17T12:05:05.720 回答