Twitter Xauth demo
Here we have tried below code for twitter xauth, but don't get proper result. Which step are we doing wrong?
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 BLO开发者_如何学CA 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
}
Did you ask Twitter to xAuth enable your app? If not, this is not going to work with xAuth.
精彩评论