2014-10-29 78 views
12

我試圖使用Twitter提供的新的面料API讓用戶登錄到我的應用程序。我完全按照教程(至少我認爲我有,也許我犯了一些錯誤)here在完成我的項目後,完成所有必要的步驟;現在,當我打的登錄按鈕和驗證按鈕還給一個成功的響應,但是當我去了之後,要獲得Twitter的會議我爬不起來,看起來像Twitter面料登錄Android的

Caused by: java.lang.IllegalStateException: Must start Twitter Kit with Fabric.with() first  

(一個例外,我跟着教程一T到目前爲止,但如果你能想到任何東西,那麼我願意嘗試它)

+0

結帳下面的鏈接:https://androidbeasts.wordpress.com/2015/07/22/twitter-fabric-integration/ – Aakash 2015-08-21 21:16:42

回答

14

Fabric SDK將功能分離到稱爲套件的模塊中。您必須通過Fabric.with()指出您希望使用的工具包。這通常是通過擴展Android的Application類來完成的。

package com.example.app; 
import android.app.Application; 

public class MyApplication extends Application { 
    @Override 
    public void onCreate() { 
     super.onCreate(); 

     TwitterAuthConfig authConfig = 
        new TwitterAuthConfig("consumerKey", 
             "consumerSecret"); 

     Fabric.with(this, new Twitter(authConfig)); 

     // Example: multiple kits 
     // Fabric.with(this, new Twitter(authConfig), 
     //     new Crashlytics()); 
    } 
} 

更多信息:https://dev.twitter.com/twitter-kit/android/integrate

見典型示例應用程序在:https://github.com/twitterdev/cannonball-android

+0

得到它的工作,非常感謝! – 2014-10-29 17:18:43

+0

嗨路易斯,你能幫我解決我的問題[這裏](http://stackoverflow.com/questions/27190313/making-rest-api-calls-from-an-android-app-using-twitterapiclient-class )...預先感謝 – 2014-11-28 14:03:10

+0

非常感謝!我使用了Android Studio自動結構集成,並完全錯過了這些小小的代碼。我需要爲客戶端更改我的API密鑰和祕密,並且不知道在哪裏可以找到它。 – LukeWaggoner 2015-03-03 12:10:03

2

最新Twitter的整合與Android工作室

這下面的鏈接提供您可以使用此示例代碼代碼整合twitter最新sdk(Fabric)。它提供的所有功能,我們可以輕鬆地集成更短的時間採取

Twitter Sample code

Reference Code Plz check it

+1

您好, 我在使用Fabric twitter登錄工具包時遇到了一些問題。從Android Studio Farbic的插件中,在選擇要安裝的套件的屏幕上,該選項是「未知」而不是「安裝」。有沒有其他人遇到過這個問題? 我不得不提一下,從零開始的項目,這不會發生。 – 2015-01-12 07:10:20

4

我的情況下,錯誤是:必須在調用Twitter的工具包

解決方案之前,Fabric.with()開始:

在此之前,我已經使用: Fabric.with(this,new Crashlytic S()); Fabric.with(this,new Twitter(authConfig)); 最後不工作。

集成Twitter的我的代碼之前是

- Fabric.with(這一點,新Crashlytics());

集成Twitter後,我用

替換

- 面料。(這個,新的Twitter(authConfig),新的Crashlytics());

現在的工作就像一個魅力,

3

這裏是我是如何實現的Twitter登錄與面料:

  1. 申報Twitter的密鑰和密碼:

    private static final String TWITTER_KEY = "r5nPFPbcDrzoJM9bIBCqyfHPK"; 
    private static final String TWITTER_SECRET = "oJ8y2KPIySPpoBX3eCcqgcnmPGXLI94BR4g9ZztnApSmXQG9Ij "; 
    
    //Twitter Login Button 
    TwitterLoginButton twitterLoginButton; 
    
  2. 的onCreate()方法:

    //Initializing TwitterAuthConfig, these two line will also added automatically while configuration we did 
    TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); 
    Fabric.with(this, new Twitter(authConfig)); 
    
    setContentView(R.layout.activity_main); 
    
    //Initializing twitter login button 
    twitterLoginButton = (TwitterLoginButton) findViewById(R.id.twitterLogin); 
    
    //Adding callback to the button 
    twitterLoginButton.setCallback(new Callback<TwitterSession>() { 
        @Override 
        public void success(Result<TwitterSession> result) { 
         //If login succeeds passing the Calling the login method and passing Result object 
         login(result); 
        } 
    
        @Override 
        public void failure(TwitterException exception) { 
         //If failure occurs while login handle it here 
         Log.d("TwitterKit", "Login with Twitter failure", exception); 
        } 
    }); 
    

3.override onActivityResult()

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     //Adding the login result back to the button 
     twitterLoginButton.onActivityResult(requestCode, resultCode, data); 
    } 

4.最後,登錄()

public void login(Result<TwitterSession> result) { 

//Creating a twitter session with result's data 
     TwitterSession session = result.data; 

     //Getting the username from session 
     final String username = session.getUserName(); 

     //This code will fetch the profile image URL 
     //Getting the account service of the user logged in 
     Twitter.getApiClient(session).getAccountService() 
       .verifyCredentials(true, false, new Callback<User>() { 
        @Override 
        public void failure(TwitterException e) { 
         //If any error occurs handle it here 
        } 

        @Override 
        public void success(Result<User> userResult) { 
         //If it succeeds creating a User object from userResult.data 
         User user = userResult.data; 

         //Getting the profile image url 
         String profileImage = user.profileImageUrl.replace("_normal", ""); 

         Log.d("done","name-->"+username + "url-->"+profileImage); 
         // Toast.makeText(this,"name-->"+username + "url-->"+profileImage,Toast.LENGTH_LONG).show(); 

        } 
       }); 
    } 

你的用戶名和profilepicture URL中login()到在任何你想要的地方使用。

+0

你能告訴我如何將用戶重定向到應用程序的twitter登錄屏幕(如果他沒有登錄),如果它已安裝或瀏覽器? – 2017-03-28 08:36:19