我正在单个活动中集成谷歌登录和Twitter登录我的应用程序,但当我点击Twitter登录按钮时,它授权帐户,但随后推回到mainActivity与下面提到的错误.Google登录工作正常,但在授权后clciking在Twitter登录按钮发回mainactivity。
Authorization completed with an error
com.twitter.sdk.android.core.TwitterAuthException: Authorize failed.
at com.twitter.sdk.android.core.identity.TwitterAuthClient.handleAuthorize(TwitterAuthClient.java:112)
at com.twitter.sdk.android.core.identity.TwitterAuthClient.authorize(TwitterAuthClient.java:103)
at com.twitter.sdk.android.core.identity.TwitterLoginButton$LoginClickListener.onClick(TwitterLoginButton.java:160)
at android.view.View.performClick(View.java:5106)
at android.view.View$PerformClick.run(View.java:20329)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(Zyg
我的主要活动代码是
public class MainActivity extends AppCompatActivity {
SignInButton signInButton;
TwitterLoginButton mLoginButton;
ProgressDialog progressDialog;
GoogleSignInClient mGoogleSignInClient;
FirebaseAuth mAuth;
private static final String TAG = "GoogleActivity";
private static final String TAGS = "TwitterLogin";
private static final int RC_SIGN_IN = 9001;
private static final int RC_TWITTER= 9002;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//COnfigure twitter sdk
TwitterAuthConfig authConfig=new TwitterAuthConfig(
getString(R.string.twitter_consumer_key),
getString(R.string.twitter_consumer_secret)
);
TwitterConfig twitterConfig=new TwitterConfig.Builder(this)
.twitterAuthConfig(authConfig)
.build();
Twitter.initialize(twitterConfig);
//config twitter
setContentView(R.layout.activity_main);
signInButton=findViewById(R.id.sign_in_btn);
//Twitter //
mLoginButton=findViewById(R.id.twitter_login);
mLoginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
Log.d(TAG, "twitterLogin:success" + result);
handleTwitterSession(result.data);
}
@Override
public void failure(TwitterException exception) {
Log.w(TAG, "twitterLogin:failure", exception);
//updateUI(null);
}
});
//Twitter//
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
//If you are trying to implement Sign In, i.e. you want to get back user identity,
// you can start with new GoogleSignInOptions(GoogleSignInOptions.DEFAULT_SIGN_IN),
//GoogleSignInOptions is options used to configure the GOOGLE_SIGN_IN_API.
//Api entry point for Google Sign In.
//You must pass your server's client ID to the requestIdToken method. To find the OAuth 2.0 client ID:
//Open the Credentials page in the API Console.
// The Web application type client ID is your backend server's OAuth 2.0 client ID.
GoogleSignInOptions gso=new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail().build();
mGoogleSignInClient= GoogleSignIn.getClient(this,gso);
// [START initialize_auth]
mAuth = FirebaseAuth.getInstance();
// [END initialize_auth]
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// ...
}
}
if (requestCode==RC_TWITTER){
// Pass the activity result to the Twitter login button.
mLoginButton.onActivityResult(requestCode, resultCode, data);
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(),null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Intent intent=new Intent(MainActivity.this,Welcome.class);
startActivity(intent);
Toast.makeText(MainActivity.this, "Successfully Signed In", Toast.LENGTH_SHORT).show();
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
// updateUI(user);
}
else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
//Snackbar.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
//updateUI(null);
}
}
});
}
// [START on_start_check_user]
@Override
protected void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
}
// [END on_start_check_user]
//twitter
private void handleTwitterSession(TwitterSession session) {
Log.d(TAG, "handleTwitterSession:" + session);
AuthCredential credential = TwitterAuthProvider.getCredential(
session.getAuthToken().token,
session.getAuthToken().secret);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
Intent twitterIntent =new Intent(MainActivity.this,Welcome.class);
startActivity(twitterIntent);
FirebaseUser user = mAuth.getCurrentUser();
// updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
//updateUI(null);
}
// ...
}
});
}
//twitter
}
在https://apps.twitter.com的应用控制台中添加以下回调方案
twittersdk://
欲了解更多信息:https://github.com/twitter/twitter-kit-android/issues/134