1

我最近在我的 android studio 中添加了 fabric 插件,但我没有在该插件上找到 twitter 登录选项。当我在 google 上搜索时,我发现了一个 问题,即 fabric 最近删除了 twitter 登录工具包。所以我使用twitter 官方文档集成了 twitterkit 。我根据twitter 开发者网站集成了 twitterkit 并实现了代码,但我遇到了问题。

这是错误日志:

E/Twitter: Failed to get app auth token
com.twitter.sdk.android.core.TwitterApiException: HTTP request failed, Status: 403
 at com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42)
 at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
 at android.os.Handler.handleCallback(Handler.java:751)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:6209)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Authorization completed with an error
com.twitter.sdk.android.core.TwitterAuthException: Authorize failed.
 at com.twitter.sdk.android.core.identity.AuthHandler.handleOnActivityResult(AuthHandler.java:98)
 at com.twitter.sdk.android.core.identity.TwitterAuthClient.onActivityResult(TwitterAuthClient.java:161)
 at com.twitter.sdk.android.core.identity.TwitterLoginButton.onActivityResult(TwitterLoginButton.java:131)
 at in.crazyfingers.cyny.social_login.ActivitySocialSignIn.onActivityResult(ActivitySocialSignIn.java:305)
 at android.app.Activity.dispatchActivityResult(Activity.java:7007)
 at android.app.ActivityThread.deliverResults(ActivityThread.java:4173)
 at android.app.ActivityThread.handleSendResult(ActivityThread.java:4220)
 at android.app.ActivityThread.-wrap20(ActivityThread.java)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1559)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:6209)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我已经检查了问题 1问题 2以及其他许多与此问题相关但对我不起作用的问题。帮助将不胜感激。提前致谢。

这是我的应用级build.gradle文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.twitter"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'

    // Include all the Twitter APIs
    compile 'com.twitter.sdk.android:twitter:3.0.0'

}

这是我的CustomApplication.Java文件。

public class CustomApplication extends Application
{
    public void onCreate() {
        Twitter.initialize(this);
    }
}

这是我的TwitterLogin.java文件

public class TwitterLogin extends AppCompatActivity
{
    public static final String TAG = "[TwitterLogin]";

    private String TWITTER_KEY = "here my app twitter key";
    private String TWITTER_SECRET = "here my app secret key";

    private TwitterLoginButton loginButton;
    private TextView status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_twitter_login);

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

        loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
        loginButton.setCallback(new LoginHandler());
    }

    private class LoginHandler extends Callback<TwitterSession> {
        @Override
        public void success(Result<TwitterSession> twitterSessionResult) {

            String output = "Status: " +
                        "Your login was successful " +
                        twitterSessionResult.data.getUserName() +
                        "nAuth Token Received: " +
                        twitterSessionResult.data.getAuthToken().token;

            Log.e(TAG, "twitterSessionResult : " + output);      
        }
        @Override
        public void failure(TwitterException e) {
            status.setText("Status: Login Failed");
        }
    }

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

1 回答 1

10

我已经搜索了一整天,我找到了以下解决方案。我不知道,但这对我来说很好用。

我从Application.java文件中注释了这一行。

//Twitter.initialize(this);

我已Twitter按如下方式初始化 SDK,它将为我工作。

//Initialize twitter SDK
TwitterConfig config = new TwitterConfig.Builder(this)
                .logger(new DefaultLogger(Log.DEBUG))
                .twitterAuthConfig(new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET))
                .debug(true)
                .build();
Twitter.initialize(config);

在推特按钮上设置callback如下。

loginButton.setCallback(new TwitterLogin.LoginHandler());
于 2017-06-16T11:58:37.420 回答