Aggressor
Aggressor

Reputation: 13551

Twitter4J Creating Callback URL That Goes Back To My App

I've read through this: Android: Login with Twitter using Twitter4J

And the selected answer doesn't make sense in the context that I need it.

I want the user to be able to login to their Twitter account from our app, and then, once they've logged in, I want our app to reload.

It loads the twitter webpage (although I wish they could just login within my app instead of needing the browser) but then I'm not sure how to set 'my application' as the callback.

Can you advise where I can find (or set if it doesnt exist) the callback URL so when the user logs into Twitter it our app re-opens?

This is my login code:

    public void loginUser()
    {
        //Check if already 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();

            try {
//What do I set my TWITTER_CALLBACK_URL as?
                requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
                this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL())));
            } catch (TwitterException e) {
                e.printStackTrace();
            }
       } else {            
       // user already logged into twitter
           Toast.makeText(getApplicationContext(),
                   "Already Logged into twitter", Toast.LENGTH_LONG).show();
        }
    }

Upvotes: 1

Views: 988

Answers (1)

Aggressor
Aggressor

Reputation: 13551

I couldnt quite figure out how the intents-in-manifest business worked.

Instead I did the following (and it works)

  1. Created a WebViewClient and overrided its shouldOverrideUrlLoading

  2. Set this WebViewClient to the webview and load the twitter login from there.

  3. In my WebView client I am parsing the twitter response looking for my string (which can be anything). If its there, I hide the webview.

    @Override
    public boolean shouldOverrideUrlLoading (WebView view, String url) {
        Log.d("Loading webview", url);
        if (url.contains(TwitterActivity.CALLBACK_URL)) {
            Uri uri = Uri.parse(url);
            String oauthVerifier = uri.getQueryParameter("oauth_verifier");
            _twitterActivty.closeTwitterBrowser(oauthVerifier);
            return false;
        }
        view.loadUrl(url);
        return true;
    }
    

Upvotes: 1

Related Questions