Android Integrated Twitter Login

Source: Internet
Author: User

Twitter has held its first developer conference in four years. This convention, called "Flight", is also the annual convention of the future.

The theme of this conference is also entirely around the developer. The focus of the conference is a new SDK called Fabric, which includes three developer kits: Twitter kitfor Twitter itself, MoPubfor the Twitter ad network, and Twitter-based The 2013 acquisition of the Mobile app Crash Analysis tool crashlytics crashlytics kit.

I'll stick with Twitter's official website: Https://dev.twitter.com/twitterkit/android/log-in-with-twitter, but it's all in English, and of course we need to climb over a wall to get online.

First we have to register the Twitter developer account and create the app, https://apps.twitter.com.

In the top right corner, click Create app:

You will then enter:

We need to fill in the name of the application, as well as the application description, as for website, the following explanation is probably:
A publicly accessible home page for your application that users can download, use, or find more information about your application. The fully qualified URL is used for the tweets created by the source application and will be displayed in the user-facing authorization screen. (If you don't have a URL yet, just place a placeholder here, but remember to change it later.) )

After the app is created, we can go to the app to see the relevant settings, click Keys and Access Tokens, you can see consumer key (API Key) and consumer Secret (API Secret), both of which need to be used

Well, here's how to create an app, and then we'll talk about how to integrate it into our project:

(a) First of all, we want to integrate Twitter-related SDK, the official online write more, if only need to login function, then only need

Write in Build.gradle (APP)

Dependencies {  ' com.twitter.sdk.android:twitter-core:3.1.1 '}

Write in the repositories of Build.gradle (project)

repositories {  jcenter ()}

(b) Add API key to our resource file, this API key can be seen in the application management of Twitter, which is what we said above two

<resources>  <string android:name= "Com.twitter.sdk.android.CONSUMER_KEY" >xxxxxxxxxxx</string >  <string android:name= "Com.twitter.sdk.android.CONSUMER_SECRET" >xxxxxxxxxxx</string></ Resources>

(iii) Establish a custom application, initialized in the OnCreate () method

Twitter.initialize (this);         New Twitterconfig.builder (this)                . Logger (new  Defaultlogger (log.debug))                . Twitterauthconfig(new twitterauthconfig ("Consumer_key", "Consumer_secret"))                . Debug (  true)                . Build ();        Twitter.initialize (config);

(d) We can use Twitter to provide a good login button, of course, can also be customized, the next will be said.

<Com.twitter.sdk.android.core.identity.TwitterLoginButton     android:id= "@+id/login_button"      android:layout_width= "Wrap_content"     android:layout_height= "Wrap_content"/>

(v) in the code:

Loginbutton = (Twitterloginbutton) Findviewbyid (R.id.login_button); Loginbutton.setcallback (New Callback<twittersession>() {   @Override   publicvoid Success (result< Twittersession> result) {       // do something with result, which provides a twittersession fo R making API calls
     result contains the user's information, we can remove tokens from it,Tokensecret
(If we have our own backend server, send these two to our own backstage, back to verify)
     Twitterauthtoken AuthToken = Result.data.getAuthToken ();

String token = Authtoken.token;
 String appId = Getresources (). getString (r.string.twitter_app_id);
String Tokensecret = Authtoken.secret;
}

 Public void  // do something on Failure  }});


@OverrideProtectedvoidOnactivityresult (int requestcodeint resultcodeintent data) {super. (requestcoderesultcode data//Pass the activity result to the login button. loginbutton. (requestcoderesultcode data        

If the login button is in fragment, then Onactivityresult should use the following code:

Should

@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {    Super. Onactivityresult (Requestcode, ResultCode, data); //Pass the activity result to the fragment, which would then Pass the result to the login//button.Fragment Fragment =Getfragmentmanager (). Findfragmentbyid (r.id.your_fragment_id); if(Fragment! =NULL) {Fragment.onactivityresult (Requestcode, ResultCode, data); }}

The rest can refer to the official website

The next one is the custom login button, in fact, there is a clever idea, see the following interface code:

<framelayout Android:id= "@+id/framelayout"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/facebook"Android:layout_margintop= "@dimen/login_button_margin_bottom"Android:layout_centerhorizontal= "true" > <Com.twitter.sdk.android.core.identity.TwitterLoginButton Android:id= "@+id/login_button"Android:layout_width= "@dimen/button_width"Android:layout_height= "@dimen/button_height"Android:layout_margintop= "@dimen/button_margin_bottom"android:visibility= "Gone"/> <ImageView Android:id= "@+id/login_image"Android:layout_width= "@dimen/button_width"Android:layout_height= "@dimen/button_height"android:src= "@drawable/twitter"/> </FrameLayout>

Then in the code:

@Override      Public void OnClick (view view) {        switch  (View.getid ()) { case r.id.login_image:                Loginbutton.performclick ();                  Break ;             default :                  Break ;        }    }

That is, when you click on our custom button, let the twitter login button perform the click action.

Come here first, then slowly Add.

Android Integrated Twitter Login

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.