Android Twitter Third party login & get user info

Source: Internet
Author: User
Tags oauth

Android Access Twitter first went to Twitter's developer site and found it too cumbersome to apply the current project to the twitter4j jar package that Twitter provided to Java.
The following are some of the experiences of access twitter4j
website address: Http://twitter4j.org/en/index.html#sourceCode which provides a download link for the jar package
Download is divided into two versions
? Latest stable version (stable version)
? Latest Snapshot build snapshot version should be updated with the times, that is, with the following source code provided in the GitHub URL to keep in sync, people according to their own needs to choose to download (for example, Bo Master download is the snapshot version, Because of the need to obtain the user's email, and this information is not in the stable version ... )
github:http://github.com/yusuke/twitter4j/

ImportAndroid.content.Intent;ImportAndroid.graphics.Bitmap;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.ViewGroup;ImportAndroid.view.ViewGroup.LayoutParams;ImportAndroid.webkit.WebView;ImportAndroid.webkit.WebViewClient;ImportCom.google.gson.Gson;ImportOrg.json.JSONException;ImportOrg.json.JSONObject;ImportJava.util.HashMap;ImportTwitter4j. Twitter;ImportTwitter4j. Twitterexception;ImportTwitter4j. Twitterfactory;ImportTwitter4j. User;ImportTwitter4j.auth.AccessToken;ImportTwitter4j.auth.RequestToken;ImportTwitter4j.conf.Configuration;ImportTwitter4j.conf.ConfigurationBuilder; Public  class twitterloginactivity extends Activity {    //Constants    StaticString Twitter_consumer_key ="Your consumer Key";StaticString Twitter_consumer_secret ="Your consumer Secret";//Preference Constants    StaticString Preference_name ="Twitter_oauth";Static FinalString Pref_key_oauth_token ="Oauth_token";Static FinalString Pref_key_oauth_secret ="Oauth_token_secret";Static FinalString Pref_key_twitter_login ="Istwitterlogedin";Static FinalString Twitter_callback_url ="Oauth://t4jsample";//Twitter OAuth URLs    Static FinalString Url_twitter_auth ="Auth_url";Static FinalString Url_twitter_oauth_verifier ="Oauth_verifier";Static FinalString Url_twitter_oauth_token ="Oauth_token";//Twitter    Private StaticTwitter Twitter;Private StaticRequesttoken Requesttoken;PrivateString verifier;PrivateGameview Gameview;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);    Startlogintwitter (); }/** * Call this method in place of the Twitter login * /     Public void Startlogintwitter() {Configurationbuilder builder =NewConfigurationbuilder ();        Builder.setoauthconsumerkey (Twitter_consumer_key);        Builder.setoauthconsumersecret (Twitter_consumer_secret);        Configuration configuration = Builder.build (); Twitterfactory factory =NewTwitterfactory (configuration); twitter = factory.getinstance ();NewThread () {@Override             Public void Run() {Try{Requesttoken = Twitter. Getoauthrequesttoken (Twitter_callback_url); }Catch(Twitterexception e)                {E.printstacktrace (); } twitterloginactivity. This. Runonuithread (NewRunnable () {@Override                     Public void Run() {if(NULL! = Requesttoken.getauthenticationurl ()) {Gameview.loadurl (Requesttoken.getauthenticationurl ())                        ;            }                    }                });    }}.start (); } Public  class gamewebviewclient extends webviewclient {        @Override         Public Boolean shouldoverrideurlloading(WebView view, String URL) {Logutil.i ("shouldoverrideurlloading:"+ URL);if(NULL! = URL) {uri uri = uri.parse (URL);if("OAuth". Equals (Uri.getscheme ()) &&"T4jsample". Equals (Uri.getauthority ())) {Handlecallback (URI);return true; }            };return false; }    }Private void Handlecallback(URI Uri) {//OAuth verifierverifier = Uri.getqueryparameter (url_twitter_oauth_verifier);NewThread () {@Override             Public void Run() {Try{//Get the access tokenAccesstoken Accesstoken = Twitter.getoauthaccesstoken (Requesttoken, verifier);NewThread (NewRunnable () {@Override                         Public void Run() {GetUserInfo (Accesstoken);                }}). Start (); }Catch(Exception e) {LOG.E ("Twitter Login Error",">"+ e.getmessage ());    }}}.start (); }Private void GetUserInfo(Accesstoken Accesstoken) {Gson Gson =NewGsonfactory (). Create (); String userId = string.valueof (Accesstoken.getuserid ());Try{User user = Twitter.showuser (Accesstoken.getuserid ());            String userimage = User.getprofileimageurl (). toString ();            String userName = User.getname ();            String usersrceenname = User.getscreenname ();            String useremail = User.getemail (); LOG.E ("Gettwitterinfo","UserId:"+ UserId +"UserName:"+ UserName +"Userimage:"+ Userimage +"Usersrceenname:"+ Usersrceenname +"UserEmail:"+ useremail); }Catch(Exception e)        {E.printstacktrace (); } hashmap<string, string> tw_resp =NewHashmap<string, string> (); Tw_resp.put ("UserId", userId); Tw_resp.put ("Userimage", userimage); Tw_resp.put ("UserName", userName); Tw_resp.put ("Usersrceenname", usersrceenname); Tw_resp.put ("UserEmail", useremail);    String jsonstring = Gson.tojson (TW_RESP); }}

user information can be seen in the users class in the Twitter4j jar package, and the following are all user-related information that can be obtained :

////Source code recreated from a. class file by IntelliJ idea//(Powered by Fernflower Decompiler)//Package Twitter4j;import Java(i). Serializable; Import Java. Util. Date; Import twitter4j. Status; Import twitter4j. Twitterresponse; Import twitter4j. Urlentity;p Ublic interface User extends Comparable<user>, Twitterresponse, Serializable {longgetId (); StringGetName (); StringGetemail (); StringGetscreenname (); StringgetLocation (); Stringgetdescription (); Booleaniscontributorsenabled (); StringGetprofileimageurl (); StringGetbiggerprofileimageurl (); StringGetminiprofileimageurl (); StringGetoriginalprofileimageurl (); StringGetprofileimageurlhttps (); StringGetbiggerprofileimageurlhttps (); StringGetminiprofileimageurlhttps (); StringGetoriginalprofileimageurlhttps (); Booleanisdefaultprofileimage (); StringGetURL (); Booleanisprotected (); IntGetfollowerscount (); StatusgetStatus (); StringGetprofilebackgroundcolor (); StringGetprofiletextcolor (); StringGetprofilelinkcolor (); StringGetprofilesidebarfillcolor (); StringGetprofilesidebarbordercolor (); Booleanisprofileusebackgroundimage (); BooleanIsdefaultprofile (); BooleanIsshowallinlinemedia (); IntGetfriendscount (); DateGetcreatedat (); IntGetfavouritescount (); IntGetUtcOffset (); StringgetTimeZone (); StringGetprofilebackgroundimageurl (); StringGetprofilebackgroundimageurlhttps (); StringGetprofilebannerurl (); StringGetprofilebannerretinaurl (); StringGetprofilebanneripadurl (); StringGetprofilebanneripadretinaurl (); StringGetprofilebannermobileurl (); StringGetprofilebannermobileretinaurl (); Booleanisprofilebackgroundtiled (); StringGetlang (); IntGetstatusescount (); Booleanisgeoenabled (); Booleanisverified (); BooleanIstranslator (); IntGetlistedcount (); Booleanisfollowrequestsent (); Urlentity[] getdescriptionurlentities (); Urlentitygeturlentity (); String[] getwithheldincountries ();}

Android Twitter Third party login & get user info

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.