android整合twitter登入

來源:互聯網
上載者:User

標籤:alt   移動   col   width   下載   mpi   代碼   3.1.1   源檔案   

Twitter曾經舉行了自己四年以來的第一場開發人員大會。而這場名為“Flight”的大會,也是以後它的年度慣例。

這次大會的主題也完全圍繞開發人員進行。大會的焦點是一個名叫Fabric的新SDK,裡麵包括三個開發人員工具包:面向Twitter本身的 Twitter Kit、面向Twitter廣告網路的MoPub,以及基於Twitter 2013年收購的行動裝置 App崩潰分析工具Crashlytics的Crashlytics Kit

我還是先貼上twitter登入的官方網站:https://dev.twitter.com/twitterkit/android/log-in-with-twitter,不過都是英文,當然,我們還需要爬過一堵牆才能夠上網。

首先我們要先去註冊twitter開發人員帳號,並且建立應用,https://apps.twitter.com.

點擊右上方建立應用:

然後會進入:

我們需要填入應用的名稱,還有應用的描述,至於website,下面解釋的意思大概是:
您的應用程式的可公開訪問的首頁,使用者可以下載,使用或尋找有關您的應用程式的更多資訊。該完整URL用於源應用程式建立的tweets,並將顯示在面向使用者的授權螢幕中。(如果你還沒有URL,只需在這裡放置一個預留位置,但記住稍後再改變它。)

 

 

應用建立完成之後,我們可以進入應用查看相關的設定,點擊Keys and Access Tokens,可以看到Consumer Key (API Key)和Consumer Secret (API Secret),這兩個需要用到

 

好了,建立應用就到這裡,接下來講講如何整合到我們的項目:

(一)首先我們要整合twitter相關的sdk,官網上寫得比較多,如果僅僅需要登入功能,那麼就只需要

在build.gradle(app)裡面寫上

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

在build.gradle(project)的 repositories裡寫上

repositories {  jcenter()}

(二)在我們的資源檔裡面添加API KEY,這個API KEY在twitter的應用管理可以看到,就是我們上面說的那兩個

<resources>  <string android:name="com.twitter.sdk.android.CONSUMER_KEY">XXXXXXXXXXX</string>  <string android:name="com.twitter.sdk.android.CONSUMER_SECRET">XXXXXXXXXXX</string></resources>

 

(三)建立一個自訂的Application,在onCreate()方法裡面初始化

 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);

 

(四)我們可以用twitter提供好的登入按鈕,當然也可以自訂,接下來會講。

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

 

(五)在代碼中:

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
     result裡麵包含了使用者的資訊,我們可以從中取出token,tokenSecret
(如果我們有自己的後台伺服器,發送這兩個到我們自己的後台,後台再去驗證)
     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);}
 

 如果登入按鈕是在fragment中的話,那麼onActivityResult應該用如下代碼:

應該

@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);    }}

其餘的可以參考官網

 

接下來講的一個是自訂登入按鈕,其實有一個妙計,請看下面介面代碼:

<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>

然後代碼中:

@Override    public void onClick(View view) {        switch (view.getId()){case R.id.login_image:                LoginButton.performClick();                break;            default:                break;        }    }

即,點擊我們自訂的按鈕的時候,讓twitter登入按鈕執行點擊操作。

先到這裡,以後慢慢補充。

 

android整合twitter登入

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.