Reputation: 24433
I am using TwitterKit v2.3.0
com.twitter.sdk.android:twitter:2.3.0@aar
Here is how I call Twitter Authentication using TwitterAuthClient
:
if (mTwitterAuthClient == null) {
TwitterAuthConfig authConfig = new TwitterAuthConfig(getString(R.string.twitter_api_key),
getString(R.string.twitter_api_secret));
Fabric.with(getActivity(), new Twitter(authConfig));
}
mTwitterAuthClient = new TwitterAuthClient();
mTwitterAuthClient.authorize(getActivity(), new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
handleTwitterSession(result.data);
}
@Override
public void failure(TwitterException exception) {
}
});
If I press login button, then press back or cancel button to close authentication screen, then press login button again, it throws an exception without showing authentication screen again:
01-13 21:34:40.689 10842-10842/com.ihnel.mclearning W/Twitter: Authorize already in progress
01-13 21:34:40.689 10842-10842/com.ihnel.mclearning E/Twitter: Authorization completed with an error
com.twitter.sdk.android.core.TwitterAuthException: Authorize failed.
at com.twitter.sdk.android.core.identity.TwitterAuthClient.handleAuthorize(TwitterAuthClient.java:110)
at com.twitter.sdk.android.core.identity.TwitterAuthClient.authorize(TwitterAuthClient.java:101)
at com.binh.auth.fragment.LoginFragment.loginWithTwitter(LoginFragment.java:157)
at com.binh.auth.databinding.AuthFragmentLoginBinding$OnClickListenerImpl2.onClick(AuthFragmentLoginBinding.java:273)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I checked the Twitter Core source code, It keeps a static instance of AuthState
, where never be renew although I recreate new instance of TwitterAuthClient.
Does someone know how to bypass this exception?
Upvotes: 2
Views: 993
Reputation: 24433
Found a similar thread in Twitter Community : https://twittercommunity.com/t/getting-authorization-error-when-cancelling-from-twitter-authorization-using-fabric-sdk/31341/10
The issue gone when I implement onActivityResult
method in my activity as below:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final TwitterAuthClient twitterAuthClient = new TwitterAuthClient();
if(twitterAuthClient.getRequestCode()==requestCode) {
twitterAuthClient.onActivityResult(requestCode, resultCode, data);
}
}
But, this just a workaround like what they said. I hope we will got a final better solution.
Upvotes: 3