2014-10-29 79 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