0

I am trying to load twitter page using twitter 4j but the twitter page is not loaded i have registered my application and get my consumer key and consumer secret key here it is my code

package a.a.a;


import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;


public class StatusListActivity extends ListActivity{


    private Otweet app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        app=(Otweet)getApplication();

        setContentView(R.layout.main);

    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if(!app.isAuthorized())
        {
        beginAuthorization();
        }
        else
        {

        }
    }
    public void beginAuthorization()
    {
        Intent intent=new Intent(StatusListActivity.this,AuthorizationActivity.class);
        startActivity(intent);
    }


}

And this is the authorization activity

package a.a.a;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class AuthorizationActivity extends Activity{


    private Otweet app;
    WebView WV;
    Twitter twitter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        app=(Otweet)getApplication();
        setContentView(R.layout.authorization_view);
        WV=(WebView)findViewById(R.id.web_view);

    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        //twitter=new TwitterFactory().getInstance();
        //twitter.setOAuthConsumer("C6lqBhtUcC5f9wuauwoxag", "BttswrGyCxaeLgOZAdtfSqlbEg1uiw6xnuXD4y2rcjE");
        String authURL=app.BeginAuthorization();
        WV.loadUrl(authURL);
    }


}

and this is the otweet activity

public class Otweet extends Application {

    private OAuthHelper authhelper;
    private Twitter twitter;
    private RequestToken currentRequestToken;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        authhelper=new OAuthHelper(this);
        twitter=new TwitterFactory().getInstance();
        authhelper.configureOAuth(twitter);

    }
    public boolean isAuthorized()
    {
        return authhelper.hasAccessToken();
    }
    public String BeginAuthorization()
    {
        try
        {
            if(null==currentRequestToken)
            {
                currentRequestToken=twitter.getOAuthRequestToken();
            }
            return currentRequestToken.getAuthorizationURL();
        }
        catch (TwitterException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null;
    }

}

The function begin authorization always returns null.

1 Answers1

0

Try to replace

authhelper.configureOAuth(twitter)

with

twitter.setOAuthConsumer(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);

If this does not work, try TwitterOAuthView.

With TwitterOAuthView, you don't have to care about the OAuth flow. Just call

view.start(CONSUMER_KEY, CONSUMER_SECRET, CALLBACK_URL, true, listener);

and receive the result via TwitterOAuthView.Listener interface defined as below.

void onSuccess(TwitterOAuthView view, AccessToken accessToken);
void onFailure(TwitterOAuthView view, TwitterOAuthView.Result result);
Takahiko Kawasaki
  • 18,118
  • 9
  • 62
  • 105