how to oauth twitter with Twitter4J api
I want to authenticate twitter with Twitter4j api but it gives following error:
09-13 12:12:09.223: WARN/System.err(677): oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.
09-13 12:12:09.233: WARN/System.err(677): at oauth.signpost.basic.DefaultOAuthProvider.retrieveToken(DefaultOAuthProvider.java:74)
09-13 12:12:09.233: WARN/System.err(677): at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:66)
09-13 12:12:09.233: WARN/System.err(677): at com.facebook.android.OAuthTwitter.onCreate(OAuthTwitter.java:45)
09-13 12:12:09.233: WARN/System.err(677): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
09-13 12:12:09.233: WARN/System.err(677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
09-13 12:12:09.233: WARN/System.err(677): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
09-13 12:12:09.233: WARN/System.err(677): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
09-13 12:12:09.243: WARN/System.err(677): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
09-13 12:12:09.243: WARN/System.err(677): at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 12:12:09.243: WARN/System.err(677): at android.os.Looper.loop(Looper.java:123)
09-13 12:12:09.243: WARN/System.err(677): at android.app.ActivityThread.main(ActivityThread.java:4203)
09-13 12:12:09.开发者_JAVA技巧243: WARN/System.err(677): at java.lang.reflect.Method.invokeNative(Native Method)
09-13 12:12:09.243: WARN/System.err(677): at java.lang.reflect.Method.invoke(Method.java:521)
09-13 12:12:09.243: WARN/System.err(677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-13 12:12:09.243: WARN/System.err(677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
09-13 12:12:09.243: WARN/System.err(677): at dalvik.system.NativeStart.main(Native method)
and i use the below code;
public class OAuthTwitter extends Activity {
public final static String CALLBACK_URL = "callback://twitter"; // ----
// (4)
private CommonsHttpOAuthConsumer commonHttpOAuthConsumer;
private OAuthProvider authProvider;
private Twitter twitter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
commonHttpOAuthConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
CONSUMER_SECRET); // ----- (5)
authProvider = new DefaultOAuthProvider(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"https://api.twitter.com/oauth/authorize");
try {
String oAuthURL = authProvider.retrieveRequestToken(
commonHttpOAuthConsumer, CALLBACK_URL);
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(oAuthURL)));
} catch (OAuthMessageSignerException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (OAuthCommunicationException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
protected void onNewIntent(Intent intent) { // ---- (6)
super.onNewIntent(intent);
Uri uri = intent.getData(); // ---- (7)
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri
.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
authProvider.retrieveAccessToken(commonHttpOAuthConsumer,
verifier); // ---- (8)
AccessToken accessToken = new AccessToken(
commonHttpOAuthConsumer.getToken(),
commonHttpOAuthConsumer.getTokenSecret()); // ---- (9)
twitter = new TwitterFactory().getInstance(); // ---- (10)
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(accessToken); // ---- (11)
// Tweet message to be updated.
String tweet = "Hiee there, This is send from my application - OAuth, Twitter";
twitter.updateStatus(tweet); // ---- (12)
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_LONG);
}
}
}
}
using the jars: signpost-commonshttp4-1.2.1.1.jar,signpost-core-1.2.jar,twitter4j-core-android-2.2.4.jar plz anyone tell me where i am wrong.
Error is of consumer key...means it may be wrong..or you have note created it properly for that you have to click on Create an app link on dev.twitter.com then after login with your twitter account you have to give name etc detail on the next page and you get the secrate key and consumer key,callback url etc... put that key in your code
精彩评论