below is code to login twitter.
requirement:
lib need to use : using twitter 4j
requirement:
lib need to use : using twitter 4j
make twitter app at dev consolve of twitter
private void parseUrl(String url) { try { if (url == null) return; if (url.startsWith(General.TWITTER_CALLBACK_URL)) { url += "&"; String oauth_verifier = com.qtm.sign2learn.utils.Util.extractPattern(url, "oauth_verifier=(.*?)&"); AccessToken accessToken = twitter.getOAuthAccessToken( requestToken, oauth_verifier); twitter.setOAuthAccessToken(accessToken); User user = twitter.verifyCredentials(); String name = user.getName(); String miniImage = user.getMiniProfileImageURL(); String bigImage = user.getBiggerProfileImageURL(); String fbUid = String.valueOf(user.getId()); Editor edit = prefs.edit(); edit.putString(General.pref_KEY_OAUTH_TOKEN, accessToken.getToken()); edit.putString(General.pref_KEY_OAUTH_SECRET, accessToken.getTokenSecret()); edit.putString(General.pref_TWITTER_ACCESS_TOKEN, accessToken.getToken()); // edit.putString(General.pref_fb_uid, fbUid); // edit.putInt(General.pref_acctype, 2); prefs.getBoolean(General.pref_KEY_TWITTER_LOGIN, true); edit.commit(); if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } loginDialog.dismiss(); } } catch (Exception e) { e.printStackTrace(); } } private class TwitterWebViewClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); parseUrl(url); progressDialog.show(); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); progressDialog.dismiss(); } } public void loginToTwitter(View v) { TwitterFactory factory = new TwitterFactory(); twitter = factory.getInstance(); twitter.setOAuthConsumer(General.TWITTER_CONSUMER_KEY, General.TWITTER_CONSUMER_SECRET); try { requestToken = twitter .getOAuthRequestToken(General.TWITTER_CALLBACK_URL); loginDialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar); loginDialog.setContentView(R.layout.webview_popup); Button btnClose = (Button) loginDialog.findViewById(R.id.btnClose); btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub loginDialog.dismiss(); } }); WebView webview = (WebView) loginDialog .findViewById(R.id.webViewVKLogin); webview.getSettings().setJavaScriptEnabled(true); webview.clearCache(true); webview.setWebViewClient(new TwitterWebViewClient()); // otherwise CookieManager will fall with // java.lang.IllegalStateException: // CookieSyncManager::createInstance() // needs to be called before CookieSyncManager::getInstance() CookieSyncManager.createInstance(this); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.removeAllCookie(); webview.loadUrl(requestToken.getAuthenticationURL()); loginDialog.show(); // this.startActivity(new Intent(Intent.ACTION_VIEW, Uri // .parse(requestToken.getAuthenticationURL()))); } catch (TwitterException e) { e.printStackTrace(); } }In general.java put below variables
// Twitter oauth urls and details public static final String TWITTER_CALLBACK_URL = "url here"; public static final String TWITTER_CONSUMER_KEY = "key here"; public static final String TWITTER_CONSUMER_SECRET = "secreat here"; public static final String URL_TWITTER_AUTH = "auth_url"; public static final String URL_TWITTER_OAUTH_VERIFIER = "oauth_verifier"; public static final String URL_TWITTER_OAUTH_TOKEN = "oauth_token"; public static final String PREFERENCE_NAME = "twitter_oauth"; public static final String pref_KEY_OAUTH_TOKEN = "sing2learn_oauth_token"; public static final String pref_KEY_OAUTH_SECRET = "sing2learn_oauth_token_secret"; public static final String pref_KEY_TWITTER_LOGIN = "sing2learn_isTwitterLogedIn"; public static final String pref_TWITTER_ACCESS_TOKEN = "sing2learn_twitter_token";
No comments :
Post a Comment