KP_
KP_

Reputation: 1866

Twitter Authentication in Android

In my app,i want to show twitter login everytime a user tried to share a post.

I dont want to save the twitter credentials in shared preference,but when i tried to share a post in second time,it is not showing the login fields.

can anyone figure out the problem?

 public class AuthorizationActivity extends Activity {

private TwitterActivity app;
private WebView webView;
static String token, verifier;

Webservice web;
OAuthCredentialsResponse credentials;
private WebViewClient webViewClient = new WebViewClient() {
    @Override
    public void onLoadResource(WebView view, String url) {
        final OAuthHmacSigner signer = new OAuthHmacSigner();
        Uri uri = Uri.parse(url);

        if (url.startsWith(Constants.OAUTH_CALLBACK_URL)) {
            try {

                if (url.indexOf("oauth_token=")!=-1) {
            token = uri.getQueryParameter("oauth_token");
            verifier = uri.getQueryParameter("oauth_verifier");

            signer.clientSharedSecret = Constants.CONSUMER_SECRET;

            OAuthGetAccessToken accessToken = new OAuthGetAccessToken(
                    Constants.ACCESS_URL);
            accessToken.transport = new ApacheHttpTransport();
            accessToken.temporaryToken = token;
            accessToken.signer = signer;
            accessToken.consumerKey = Constants.CONSUMER_KEY;
            accessToken.verifier = verifier;

             credentials = accessToken.execute();

            signer.tokenSharedSecret = credentials.tokenSecret;


                    + credentials.tokenSecret);

            ;
            if (null != token) {
                webView.setVisibility(View.INVISIBLE);


                finish();

            } else if (url.indexOf("error=")!=-1) {
                view.setVisibility(View.INVISIBLE);

            }

                }} catch (IOException e) {
            e.printStackTrace();
        }

    }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.authorization_view);
    setUpViews();

String authURL = beginAuthorization();
webView.loadUrl(authURL);
}



private void setUpViews() {
    webView = (WebView) findViewById(R.id.web_view);
    webView.setWebViewClient(webViewClient);
}
public String beginAuthorization() {
    try {

        final OAuthHmacSigner signer = new OAuthHmacSigner();
        OAuthAuthorizeTemporaryTokenUrl authorizeUrl;
        signer.clientSharedSecret = Constants.CONSUMER_SECRET;

    OAuthGetTemporaryToken temporaryToken = new   OAuthGetTemporaryToken(Constants.REQUEST_URL);
        temporaryToken.transport = new ApacheHttpTransport();
        temporaryToken.signer = signer;
        temporaryToken.consumerKey = Constants.CONSUMER_KEY;
        temporaryToken.callback = Constants.OAUTH_CALLBACK_URL;

        OAuthCredentialsResponse tempCredentials =                        temporaryToken.execute();
        signer.tokenSharedSecret = tempCredentials.tokenSecret;

     authorizeUrl = new OAuthAuthorizeTemporaryTokenUrl(Constants.AUTHORIZE_URL);
        authorizeUrl.temporaryToken = tempCredentials.token;
        String authorizationUrl = authorizeUrl.build();
        System.out.println(authorizationUrl);

        return authorizationUrl;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

public void authorized() {
    try {



        AccessToken a = new AccessToken(credentials.token,credentials.tokenSecret);
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
        twitter.setOAuthAccessToken(a);
        twitter.updateStatus("my first post");

        Toast t = Toast.makeText(this,"Successfully Shared",Toast.LENGTH_LONG);
        t.show();

    } catch (TwitterException e) {
        throw new RuntimeException("Unable to authorize user", e);
    }
}

Upvotes: 1

Views: 796

Answers (1)

MKJParekh
MKJParekh

Reputation: 34301

I think you missed one parameter in the URL

that's force_login = true , This will ask user to login even the session is not expired.

To read more go to this link RestAPI

Upvotes: 3

Related Questions