Android integrates twitter login, android integrates twitter

Source: Internet
Author: User

Android integrates twitter login, android integrates twitter

Twitter once held its first Developer Conference in four years. This conference named "Flight" is also its annual practice in the future.

The theme of this conference is fully centered on developers. The focus of the conference is a new SDK named Fabric, which includes three developer toolkits: for TwitterTwitter Kit, For Twitter advertising networkMoPubAnd mobile app crash analysis tools acquired by Twitter in 2013Crashlytics's Crashlytics Kit.

I should post the official twitter website: twitter.

First we need to first register twitter developer account, and create application, https://apps.twitter.com.

Click Create application in the upper right corner:

Then enter:

We need to enter the name of the application and the description of the application. As for the website, the following explanation means:
The homepage for public access to your application. You 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 on the user-facing authorization screen. (If you do not have a URL, place a placeholder here, but remember to change it later .)

 

 

After the application is created, we can go to the application to view the relevant settings. Click Keys and Access Tokens to see the Consumer Key (API Key) and Consumer Secret (API Secret), which need to be used.

 

Now, let's talk about how to integrate the application into our project:

(1) first, we need to integrate twitter-related sdks. Many sdks are written on the official website. If you only need the login function, you only need

Write in build. gradle (app)

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

Write in the repositories of build. gradle (project)

repositories {  jcenter()}

(2) Add an api key to our resource file. This api key can be seen in the Application Management of twitter, that is, the 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>

 

(3) create a custom Application and initialize it in the onCreate () method.

 Twitter.initialize(this);        TwitterConfig config = new TwitterConfig.Builder(this)                .logger(new DefaultLogger(Log.DEBUG))                .twitterAuthConfig(new TwitterAuthConfig("CONSUMER_KEY", "CONSUMER_SECRET"))                .debug(true)                .build();        Twitter.initialize(config);

 

(4) We can use the logon button provided by twitter, which can also be customized. Next we will talk about it.

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

 

(5) In the Code:

loginButton = (TwitterLoginButton) findViewById(R.id.login_button);loginButton.setCallback(new Callback<TwitterSession>() {   @Override   public void success(Result<TwitterSession> result) {       // Do something with result, which provides a TwitterSession for making API calls
The result contains the user information. We can retrieve the token and tokenSecret from the result.
(If we have our own backend servers, send these two servers to our own backend and verify them in the backend)
     TwitterAuthToken authToken = result.data.getAuthToken();

String token = authToken.token;
 String appId = getResources().getString(R.string.twitter_app_id);
String tokenSecret = authToken.secret;
  }

 @Override public void failure(TwitterException exception) { // Do something on failure  } });


@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    // Pass the activity result to the login button.    loginButton.onActivityResult(requestCode, resultCode, data);}
 

If the logon button is in fragment, the onActivityResult should use the following code:

Should

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    // Pass the activity result to the fragment, which will 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);    }}

For more information, see the official website.

 

Next, we will talk about the custom logon button. In fact, there is a clever solution. Please refer to 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 the Custom button, let the twitter login button perform the click operation.

First come here, and then add it later.

 

Related Article

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.