代码似乎有效。 Facebook和电子邮件登录工作。当我点击twitter按钮时,它会将我带到twitter登录和授权页面,我登录,然后没有任何反应。日志未显示成功,控制台未显示新用户。也没有错误。这是登录片段。对不起,如果它很乱,但我不是很有经验。
public class Login_Fragment extends Fragment implements OnClickListener {
private View view;
private EditText editTextEmail, editTextPassword;
private Button loginButton;
private TextView forgotPassword, signUp;
private CheckBox show_hide_password, remember_me;
private LinearLayout loginLayout;
private static Animation shakeAnimation;
private FirebaseAuth.AuthStateListener mAuthListener;
FragmentManager manager;
private FirebaseAuth mAuth;
private TwitterLoginButton mLoginButton;
private static final String TAG = "TwitterLogin";
public Login_Fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TwitterAuthConfig authConfig = new TwitterAuthConfig(
getString(R.string.twitter_consumer_key),
getString(R.string.twitter_consumer_secret));
Fabric.with(getActivity(), new com.twitter.sdk.android.Twitter(authConfig), new Twitter(authConfig));
view = inflater.inflate(R.layout.login_layout, container, false);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
manager = getFragmentManager();
mAuth = FirebaseAuth.getInstance();
initViews();
setListeners();
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);
}
});
return view;
}
@Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
}
@Override
public void onResume(){
super.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = getFragmentManager().findFragmentByTag(Utils.Login_Fragment);
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
private void handleTwitterSession(TwitterSession session) {
Log.d(TAG, "handleTwitterSession:" + session);
AuthCredential credential = TwitterAuthProvider.getCredential(
session.getAuthToken().token,
session.getAuthToken().secret);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInWithCredential:success");
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
BuySell_Fragment frag = new BuySell_Fragment();
ft.replace(R.id.frameContainer, frag);
ft.addToBackStack(null);
ft.commit();
} else {
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(getContext(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
private void initViews() {
editTextEmail = (EditText) view.findViewById(R.id.login_emailid);
editTextPassword = (EditText) view.findViewById(R.id.login_password);
loginButton = (Button) view.findViewById(R.id.loginBtn);
forgotPassword = (TextView) view.findViewById(R.id.forgot_password);
signUp = (TextView) view.findViewById(R.id.createAccount);
show_hide_password = (CheckBox) view
.findViewById(R.id.show_hide_password);
loginLayout = (LinearLayout) view.findViewById(R.id.login_layout);
remember_me = (CheckBox) view.findViewById(R.id.remember_me);
mLoginButton = (TwitterLoginButton) view.findViewById(R.id.button_twitter_login);
shakeAnimation = AnimationUtils.loadAnimation(getActivity(),
R.anim.shake);
}
// Set Listeners
private void setListeners() {
loginButton.setOnClickListener(this);
forgotPassword.setOnClickListener(this);
signUp.setOnClickListener(this);
show_hide_password.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button,
boolean isChecked) {
if (isChecked) {
show_hide_password.setText(R.string.hide_pwd);// change
editTextPassword.setInputType(InputType.TYPE_CLASS_TEXT);
editTextPassword.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());
} else {
show_hide_password.setText(R.string.show_pwd);
editTextPassword.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
editTextPassword.setTransformationMethod(PasswordTransformationMethod
.getInstance());
}
}
});
}
@Override
public void onClick(View v) {
String email = editTextEmail.getText().toString();
String password = editTextPassword.getText().toString();
switch (v.getId()) {
case R.id.loginBtn:
if (email.isEmpty() || password.isEmpty()) {
new CustomToast().Show_Toast(getActivity(), view,
"المرجو ادخال ايميل و كلمة سر");
} else {
checkValidation();
signIn(email, password);
}
break;
case R.id.forgot_password:
android.support.v4.app.FragmentTransaction ft =
getActivity().getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ForgotPassword_Fragment frag = new ForgotPassword_Fragment();
ft.replace(R.id.frameContainer, frag);
ft.addToBackStack(null);
ft.commit();
break;
case R.id.createAccount:
android.support.v4.app.FragmentTransaction fpft =
getActivity().getSupportFragmentManager().beginTransaction();
fpft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
SignUp_Fragment fpfrag = new SignUp_Fragment();
fpft.replace(R.id.frameContainer, fpfrag);
fpft.addToBackStack(null);
fpft.commit();
}
}
private void checkValidation() {
String getEmailId = editTextEmail.getText().toString();
String getPassword = editTextPassword.getText().toString();
Pattern p = Pattern.compile(Utils.regEx);
Matcher m = p.matcher(getEmailId);
// Check for both field is empty or not
if (getEmailId.equals("") || getEmailId.length() == 0
|| getPassword.equals("") || getPassword.length() == 0) {
loginLayout.startAnimation(shakeAnimation);
new CustomToast().Show_Toast(getActivity(), view,
"جميع الخانات مطلوبة");
}
else if (!m.find())
new CustomToast().Show_Toast(getActivity(), view,
"الايميل غير صحيح");
}
private void signIn(String email, String password) {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
new CustomToast().Show_Toast(getActivity(), view,
"محاولة تسجبل الدخول فشلت");
} else {
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
BuySell_Fragment frag = new BuySell_Fragment();
ft.replace(R.id.frameContainer, frag);
ft.addToBackStack(null);
ft.commit();
}
}
});
}
@Override
public void onDetach() {
super.onDetach();
try {
Field fragmentManager = Fragment.class.getDeclaredField("mFragmentManager");
fragmentManager.setAccessible(true);
fragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
答案 0 :(得分:0)
你做了所有的后端工作吗?我假设您在auth面板中启用了twitter登录时已将API secrete和API密钥添加到firebase控制台,但您是否也转到apps.twitter.com并添加了OAuth重定向URI?
答案 1 :(得分:0)
将Fragment中的ActivityResult传递给TwitterLoginButton
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mLoginButton.onActivityResult(requestCode, resultCode, data);
}
在膨胀这些片段的活动中,使用以下代码将结果从twitter意图传递给子片段
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Fragment fragment = (Fragment) getChildFragmentManager().findFragmentByTag(childTag);
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, intent);
}
}