i have %&·$5#~€ problem connection twitter. (the code post below this)
first, have configured (twitter keys, callback in manifest, etc), call twitter , browser open, signin in twitter , accept application, browser returns application , try response twitter, null answer.
can me find what's going on this?
grettings
pd: follow tutorial : http://www.androidhive.info/2012/09/android-twitter-oauth-connect-tutorial/
pd 2 : people thinks problem date&time of phone (https://dev.twitter.com/discussions/374), changed , doesn't work
import twitter4j.twitter; import twitter4j.twitterexception; import twitter4j.twitterfactory; import twitter4j.user; import twitter4j.auth.accesstoken; import twitter4j.auth.requesttoken; import twitter4j.conf.configuration; import twitter4j.conf.configurationbuilder; import android.annotation.suppresslint; import android.app.activity; import android.app.progressdialog; import android.content.intent; import android.content.sharedpreferences; import android.content.sharedpreferences.editor; import android.net.uri; import android.os.asynctask; import android.os.build; import android.os.bundle; import android.os.strictmode; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.imageview; import android.widget.toast; @suppresslint("newapi") public class twitteractivity extends activity { intent twitter_intent = null; //twitter things static string twitter_consumer_key = "consumer_key_here"; static string twitter_consumer_secret = "consumer_secret_here"; static final string twitter_callback_url = "oauth://t4jsample"; // twitter oauth urls static final string url_twitter_auth = "https://api.twitter.com/oauth/authorize"; static final string url_twitter_oauth_verifier = "https://api.twitter.com/oauth/access_token"; static final string url_twitter_oauth_token = "https://api.twitter.com/oauth/request_token"; // progress dialog progressdialog pdialog; // twitter public static twitter twitter; public static string twitter_token, twitter_secret; // internet connection detector private connectiondetector cd; // alert dialog manager alertdialogmanager alert = new alertdialogmanager(); // preference constants static string preference_name = "twitter_oauth"; static final string pref_key_oauth_token = "oauth_token"; static final string pref_key_oauth_secret = "oauth_token_secret"; static final string pref_key_twitter_login = "istwitterlogedin"; // twitter private static requesttoken requesttoken; // shared preferences private static sharedpreferences msharedpreferences; @suppresslint("newapi") @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_twitter); if (build.version.sdk_int > 9) { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); } //setrequestedorientation(activityinfo.screen_orientation_portrait); cd = new connectiondetector(getapplicationcontext()); // check if internet present if (!cd.isconnectingtointernet()) { // internet connection not present alert.showalertdialog(twitteractivity.this, "internet connection error","please connect working internet connection", false); // stop executing code return return; } // check if twitter keys set if(twitter_consumer_key.trim().length() == 0 || twitter_consumer_secret.trim().length() == 0){ // internet connection not present alert.showalertdialog(twitteractivity.this, "twitter oauth tokens", "please set twitter oauth tokens first!", false); // stop executing code return return; } // shared preferences msharedpreferences = getapplicationcontext().getsharedpreferences("mypref", 0); imageview tw = (imageview) findviewbyid(r.id.twitter_boton); tw.setclickable(true); tw.setonclicklistener(new onclicklistener(){ public void onclick(view v){ logintotwitter(); } }); /** if conditions tested once * redirected twitter page. parse uri oauth * verifier * */ if (!istwitterloggedinalready()) { uri uri = getintent().getdata(); if (uri != null && uri.tostring().startswith(twitter_callback_url)) { // oauth verifier string verifier = uri.getqueryparameter(url_twitter_oauth_verifier); try { // access token accesstoken accesstoken = twitter.getoauthaccesstoken( requesttoken, verifier); // shared preferences editor e = msharedpreferences.edit(); // after getting access token, access token secret // store them in application preferences e.putstring(pref_key_oauth_token, accesstoken.gettoken()); e.putstring(pref_key_oauth_secret, accesstoken.gettokensecret()); // store login status - true e.putboolean(pref_key_twitter_login, true); e.commit(); // save changes log.e("twitter oauth token", "> " + accesstoken.gettoken()); // getting user details twitter // getting name long userid = accesstoken.getuserid(); user user = twitter.showuser(userid); string username = user.getname(); log.d("nombre",username); } catch (exception e) { // check log login errors log.e("twitter login error", "> " + e.tostring()); log.e("twitter login error", "> " + e.getmessage()); } } } } /** * function login twitter * */ private void logintotwitter() { // check if logged in if (!istwitterloggedinalready()) { configurationbuilder builder = new configurationbuilder(); builder.setoauthconsumerkey(twitter_consumer_key); builder.setoauthconsumersecret(twitter_consumer_secret); configuration configuration = builder.build(); twitterfactory factory = new twitterfactory(configuration); twitter = factory.getinstance(); if(!(build.version.sdk_int >= build.version_codes.honeycomb)) { try { requesttoken = twitter.getoauthrequesttoken(twitter_callback_url); this.startactivity(new intent(intent.action_view, uri.parse(requesttoken.getauthenticationurl()))); } catch (twitterexception e) { e.printstacktrace(); } } else { new thread(new runnable() { public void run() { try { requesttoken = twitter.getoauthrequesttoken(twitter_callback_url); twitteractivity.this.startactivity(new intent(intent.action_view, uri.parse(requesttoken.getauthenticationurl()))); } catch (twitterexception e) { e.printstacktrace(); } } }).start(); } } else { // user logged twitter toast.maketext(getapplicationcontext(), "already logged twitter", toast.length_long).show(); } } /** * function update status * */ class updatetwitterstatus extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(twitteractivity.this); pdialog.setmessage("updating twitter..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } /** * getting places json * */ protected string doinbackground(string... args) { log.d("tweet text", "> " + args[0]); string status = args[0]; try { configurationbuilder builder = new configurationbuilder(); builder.setoauthconsumerkey(twitter_consumer_key); builder.setoauthconsumersecret(twitter_consumer_secret); // access token string access_token = msharedpreferences.getstring(pref_key_oauth_token, ""); // access token secret string access_token_secret = msharedpreferences.getstring(pref_key_oauth_secret, ""); accesstoken accesstoken = new accesstoken(access_token, access_token_secret); twitter twitter = new twitterfactory(builder.build()).getinstance(accesstoken); // update status twitter4j.status response = twitter.updatestatus(status); log.d("status", "> " + response.gettext()); } catch (twitterexception e) { // error in updating status log.d("twitter update error", e.getmessage()); } return null; } /** * after completing background task dismiss progress dialog , show * data in ui use runonuithread(new runnable()) update ui * background thread, otherwise error * **/ protected void onpostexecute(string file_url) { // dismiss dialog after getting products pdialog.dismiss(); // updating ui background thread runonuithread(new runnable() { @override public void run() { toast.maketext(getapplicationcontext(), "status tweeted successfully", toast.length_short) .show(); // clearing edittext field } }); } } /** * check user logged in application using twitter login flag * fetched shared preferences * */ private boolean istwitterloggedinalready() { // return twitter login status shared preferences return msharedpreferences.getboolean(pref_key_twitter_login, false); } protected void onresume() { super.onresume(); } }
after spending hours reading documentation (i don't have eyes now), when try access_token call this:
// access token accesstoken accesstoken = twitter.getoauthaccesstoken(requesttoken, verifier);
i pass verifier( pin pass, pin pass isn't use anymore ) , null because doesn't use anymore twitter, need rewrite this:
// access token accesstoken accesstoken = twitter.getoauthaccesstoken(requesttoken);
only need requesttoken access_token.
i hope helping have same problem.
grettings.
Comments
Post a Comment