androidtwitter-oauthandroid-twitter

Twitter Integration with android oAuth Issue ?


I am trying to integrate Twitter login for my android app from this URL http://androidcodeexamples.blogspot.in/2011/12/how-to-integrate-twitter-in-android.html

It seems to log it in but somehow cannot generate Access token What I belive till now is that maybe twitter has modified its access token url......

private static final String TWITTER_ACCESS_TOKEN_URL = "http://api.twitter.com/oauth/access_token";
private static final String TWITTER_AUTHORZE_URL = "https://api.twitter.com/oauth/authorize";
private static final String TWITTER_REQUEST_URL = "https://api.twitter.com/oauth/request_token";

So can anyone help on what url to set for access token


Solution

  • Try that

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
                case HIDE_PROGRESS_DIALOG:{
                    hideProgressDialog();
                }break;
                case PROCESS_TOKEN:{
                    processToken(url);
                }break;
            }
        }
    }
    
    @Override
    public void create() {
        LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        mainLayout.setBackgroundColor(Color.parseColor("#ffffff"));
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT, 
                LinearLayout.LayoutParams.FILL_PARENT);
        mainLayout.setLayoutParams(lp);
    
        webView = new WebView(this);
        webView.setLayoutParams(lp);
    
        mainLayout.addView(webView);
    
        setContentView(mainLayout);
    
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:{
                    }break;    
                    case MotionEvent.ACTION_UP:{
                        if (!v.hasFocus()) {
                            v.requestFocus();
                        }
                    }break;
                }
                return false;
            }
        });
        webView.setWebViewClient(new WebViewClient(){
    
            @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                }
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
    
            if (url.startsWith(CALLBACK_URL)) {
                    TwitterAuthorizationActivity.this.url = url;
    
                    mHandler.sendEmptyMessage(PROCESS_TOKEN);
    
                    return true;
            }else if (url.startsWith("authorize")) {
                    return false;
            }
    
                return true;
            }
    
            @Override
            public void onPageFinished(WebView view, String url) {
            }
    
    
    
        });
    
        mTwitter = new TwitterFactory().getInstance();
    
        mHttpOauthConsumer = new CommonsHttpOAuthConsumer(twitterConsumerKey, twitterSecretKey);
        mHttpOauthprovider = new CommonsHttpOAuthProvider("https://twitter.com/oauth/request_token",
                "https://twitter.com/oauth/access_token",
                "https://twitter.com/oauth/authorize");
    
        try{
            authUrl = mHttpOauthprovider.retrieveRequestToken(mHttpOauthConsumer, CALLBACK_URL);
        }catch(OAuthCommunicationException oACEx){
            Log.d("", "");
        }catch(OAuthMessageSignerException oAMSEx){
            Log.d("", "");
        }catch(OAuthNotAuthorizedException oANAEx){
            Log.d("", "");
        }catch(OAuthExpectationFailedException oAEFEx){
            Log.d("", "");
        }
    
        webView.loadUrl(authUrl);
    }
    
    public void processToken(String callbackUrl)  {
        final String verifier = getVerifier(callbackUrl);
    
        int what = 1;
    
        try {
            mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer, verifier);
    
            mAccessToken = new AccessToken(mHttpOauthConsumer.getToken(), mHttpOauthConsumer.getTokenSecret());
    
            mTwitter.setOAuthConsumer(twitterConsumerKey, twitterSecretKey);
    
            mTwitter.setOAuthAccessToken(mAccessToken);
    
            what = 0;
        } catch (Exception e){
        }
    }
    
    private String getVerifier(String callbackUrl) {
        String verifier  = "";
    
        try {
            callbackUrl = callbackUrl.replace("twitterapp", "http");
    
            URL url = new URL(callbackUrl);
            String query = url.getQuery();
    
            String array[] = query.split("&");
    
            for (String parameter : array) {
                String v[] = parameter.split("=");
    
                if (URLDecoder.decode(v[0]).equals(oauth.signpost.OAuth.OAUTH_VERIFIER)) {
                    verifier = URLDecoder.decode(v[1]);
                    break;
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    
        return verifier;
    }
    

    This solution using Twitter4J library.