Reputation: 13910
what is the conventional way of dealing with this new type of authentication twitter has implemented. I don't think I am doing it it correctly, right now I have this activity:
So the user in order to authenticate the app they have to get a pin which they have to get by clicking on get Pin button.
which has this code:
@
Override
protected String doInBackground(Void...params) {
String s = null;
twitter = TwitterFactory.getSingleton();
twitter.setOAuthConsumer("XXXX", "XXXXX");
try {
requestToken = twitter.getOAuthRequestToken();
s = requestToken.getAuthorizationURL();
} catch (TwitterException e) {
e.printStackTrace();
}
return s;
}
@
Override
protected void onPostExecute(String url) {
Intent intent = new Intent(AuthenticateActivity.this, Oauth_web_view.class);
intent.putExtra("url", url);
startActivity(intent);
}
}
that takes them to another activity which is just a webview and lets them get their pin, after that they input their pin, they have to submit pin which they press the submit pin button there's this code.
class AddUserCreds extends AsyncTask < Void, String, String > {
@
Override
protected String doInBackground(Void...urls) {
//String url = urls[0];
String pin = txtPin.getText().toString();
System.out.println(pin);
// try{
// if(pin.length() > 0){
try {
accessToken = twitter.getOAuthAccessToken(requestToken, pin);
// 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.toString());
// Store login status - true
e.putBoolean("PREF_KEY_TWITTER_LOGIN", true);
e.commit(); // save changes
Log.e("Twitter OAuth Token", "> " + accessToken.getToken());
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// }
// } catch (TwitterException te) {
// if(401 == te.getStatusCode()){
// // System.out.println("Unable to get the access token.");
// }else{
// te.printStackTrace();
// }
// }
return "done";
//
}
@
Override
protected void onPostExecute(String url) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(AuthenticateActivity.this);
alertDialogBuilder.setMessage("You've been authenticated" + url);
alertDialogBuilder.show();
}
it worked once and it added my twitter dev account to a fake account I made but now if I run it again it breaks because it's already authenticated. Is there a way to test if I am authenticated already ? so they do not have to go through this process ? Is there a more streamlined process of getting authentication ?
Upvotes: 0
Views: 380
Reputation: 150
What I do is to first read the auth toekn/secret from the preferences (which I saved during the first authentication, as you already do), and if I see it was saved, I use it instead of performing an additional auth attempt. See below a snippet of my code, hope it helps:
String authToken = prefs.getString(PREF_TWITTER_ACCESS_TOKEN, null);
String authTokenSecret = prefs.getString(PREF_TWITTER_ACCESS_TOKEN_SECRET, null);
if (authToekn == null)
{
//... perform auth
}
else //we're ready to go
{
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(APIKEY)
.setOAuthConsumerSecret(APISECRET)
.setOAuthAccessToken(authToken)
.setOAuthAccessTokenSecret(authTokenSecret);
twitterConnection = new TwitterFactory(cb.build()).getInstance();
}
Upvotes: 1