Facebook RegisterCallback无法正常运作

时间:2019-07-18 16:14:35

标签: java android facebook android-studio facebook-graph-api

我正在制作一个名为“ BrandShopping”的应用。该应用程序旨在用于在线交付和购物。这就是为什么我需要不同的登录方法,以便用户可以选择自己认为最舒适的方式。

我遇到的问题是我的Facebook登录名。我不太了解自己在做什么错。我主要在使用RegisterCallback()函数时遇到问题。

问题是使用登录按钮(在本例中为RegisterCallback())将应用指定为RegisterWithFacebookBtn。在OnSuccess()方法中,我指定了应用程序以调用名为FetchUserData()的方法,该方法应该从Facebook数据库中读取用户的信息。在该方法的稍后部分,我使应用程序检查了用户是否登录。如果这是truegoto AccountinfoActivity(另一项活动),但从未执行。

我已经尝试调试应用程序,但是我不明白程序有什么问题。我确实有一个祝酒词,如果registerCallback()成功了,该祝酒词会显示,但它不会显示祝酒词。

我的代码如下:

public class LoginOrSignupActivity extends AppCompatActivity {

    private Button LoginBtn, RegisterWithEmailBtn, RegisterWithGoogleBtn;
    private String Tag;
    private ProgressDialog LoadingBar;
    private LoginButton RegisterWithFacebookBtn;

    private FirebaseDatabase firebasedatabase = 
FirebaseDatabase.getInstance();
    private DatabaseReference database = firebasedatabase.getReference();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_or_signup);

        LoadinGUI(); //LOAD IN THE GRAPHICAL USER INTERFACE

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
          .requestIdToken(getString(R.string.default_web_client_id))
          .requestEmail()
          .build();

        LoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginOrSignupActivity.this, LogInActivity.class));
            }
        });

        RegisterWithEmailBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginOrSignupActivity.this, RegisterActivity.class));
            }
        });


        RegisterWithFacebookBtn.setOnClickListener(new View.OnClickListener() { //REGISTRATION WITH FACEBOOK
            @Override
            public void onClick(View view) {
              SignInWithFacebook();
            }
        });
    }

    private void SignInWithFacebook(){ //FOR FACEBOOK LOGIN

        CallbackManager callbackManager = CallbackManager.Factory.create();

        RegisterWithFacebookBtn.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                    Toast.makeText(LoginOrSignupActivity.this, "Part one succeded", Toast.LENGTH_SHORT).show();
                    AccessToken accessToken = loginResult.getAccessToken();
                    FetchUserData(accessToken);
            }

            @Override
            public void onCancel() {
            }

            @Override
            public void onError(FacebookException error) {
            }
        });
    }

    // FOR FACEBOOK LOGIN, ALSO SAVES TO THE FIREBASE DATABSE
    private void FetchUserData(final AccessToken accessToken){ 
        GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(
              JSONObject object, GraphResponse response
            ) {
              try {
                String name = object.getString("name");
                String email = object.getString("email");
                // ADD MORE HERE

                /* WHEN EVERYTHING IS DONE AND SUCCEEDS, 
                 *   GOTO ACCOUNTINFOACTIVITY ACTIVITY; MAY NEED TO BE
                 * MODIFIED LATER ON 
                 */
                if(accessToken != null) {
                  startActivity(new Intent(LoginOrSignupActivity.this, AccountInfoActivity.class));
                }
              } catch (JSONException e) {
                e.printStackTrace();
              }
            }
        });

        Bundle parameters = new Bundle();
        parameters.putString("fileds", "id,name,emai");
    }

任何帮助都将不胜感激

1 个答案:

答案 0 :(得分:1)

您是否已在“活动”中添加了此内容?

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }