Twitter Xauth演示

时间:2011-04-18 11:32:48

标签: android xauth

这里我们尝试了下面的twitter xauth代码,但没有得到正确的结果。我们哪一步做错了?

package com.test.twitter;

import org.apache.http.client.HttpClient;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.http.AccessToken;
import twitter4j.http.OAuthAuthorization;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class BLOA extends ListActivity implements OnClickListener {
    public static final String TAG = "BLOA";

    private CheckBox mCB;       
    private Button mButton;

    public String mToken;
    public String mSecret;

    SharedPreferences mSettings;

    HttpClient mClient;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mCB = (CheckBox) this.findViewById(R.id.enable);
        mCB.setChecked(false);

        mButton = (Button) this.findViewById(R.id.post);

        mButton.setOnClickListener(this);
        mCB.setOnClickListener(this);

    }

    public void onClick(View v) {
          if(mButton.equals(v)) {

              String consumer_key = "consumerkey";
              String consumer_secret = "secreatkey";

              String ID ="userid";
              String Password = "password";
              String message = "hello";

                try {    

                         AccessToken accessToken = null;                                                 
                         ConfigurationBuilder builder = new   ConfigurationBuilder ();    
                         builder.setOAuthConsumerSecret (consumer_secret);    
                        twitter4j.conf.Configuration config =  builder.build();                     
                        OAuthAuthorization oauth = new OAuthAuthorization (config, consumer_key, consumer_secret);                  
                       accessToken =  oauth.getOAuthAccessToken (ID, Password);    
                       TwitterFactory facrory = new TwitterFactory(config);                     
                      Twitter twitter = facrory.getOAuthAuthorizedInstance (accessToken);    
                      Status status = twitter.updateStatus(message);     
                      Log.d(" TwitterTest " , " Success updated status : " + status.getText()); 

                }                
                  catch (TwitterException e) {                        

                      Log.d(" TwitterTest " , " Failed : " + e.getMessage()); 
              } 

        }
    }


    // Get stuff from the two types of Twitter JSONObject we deal with: credentials and status 

    // These parameters are needed to talk to the messaging service

    //----------------------------
    // This task is run on every onResume(), to make sure the current credentials are valid.
    // This is probably overkill for a non-educational program



}

1 个答案:

答案 0 :(得分:1)

您是否曾要求Twitter为xAuth启用您的应用?如果没有,这对xAuth不起作用。