Facebook登录后,使用Parse登录返回ParseUser == null

时间:2015-06-23 07:28:41

标签: android facebook login parse-platform

我目前正在使用Parse开发一个Android应用程序。我的应用程序的一部分是社交登录组件。我可以正常启动Facebook登录对话框并将其与我的facebook凭据一起使用。 Facebook登录完成后,没有任何反应。我查看了调试器,发现Facebook登录后ParseUser变量仍然为空。这是我的班级代码。

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.myapp.myapp_app.NavigationDraw;
import com.myapp.myapp_app.R;
import com.myapp.myapp_app.Welcome;
import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.parse.SignUpCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.List;

public class LoginSignupActivity extends Fragment {

private static final String ARG_SECTION_NUMBER = "section_number";

public static LoginSignupActivity newInstance(int sectionNumber) {
    LoginSignupActivity logsign = new LoginSignupActivity();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    return logsign;
}

private Dialog progressDialog;

//Facebook-Login
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
}
Button loginbutton;
Button signup;
Button facebookloginbutton;
String usernametxt;
String passwordtxt;
EditText password;
EditText username;
ParseUser user = new ParseUser();


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)  {

    super.onCreate(savedInstanceState);

    View rootView = inflater.inflate(R.layout.loginsignup, container, false);

    username = (EditText) rootView.findViewById(R.id.username);
    password = (EditText) rootView.findViewById(R.id.password);

    loginbutton = (Button) rootView.findViewById(R.id.login);
    signup = (Button) rootView.findViewById(R.id.signup);
    facebookloginbutton = (Button) rootView.findViewById(R.id.facebook_login);

    loginbutton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {

            usernametxt = username.getText().toString();
            passwordtxt = password.getText().toString();

            ParseUser.logInInBackground(usernametxt, passwordtxt,
                    new LogInCallback() {
                        public void done(ParseUser user, ParseException e) {
                            if (user != null) {
                                // Fragmentwechsel
                                final FragmentTransaction ft = getFragmentManager().beginTransaction();
                                ft.replace(R.id.container, new Welcome(), "NewFragmentTag");
                                ft.commit();
                            } else {

                            }
                        }
                    });
        }
    });
    signup.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // Fragmentwechsel
            final FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.container, new SignupActivity(), "NewFragmentTag");
            ft.addToBackStack(null);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.commit();
        }
    });

    facebookloginbutton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            List<String> permissions = Arrays.asList("public_profile", "email");

            ParseFacebookUtils.logInWithReadPermissionsInBackground(getActivity(), permissions, new LogInCallback() {
                @Override
                public void done(ParseUser user, ParseException err) {
                    if (user == null) {
                        Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
                    } else if (user.isNew()) {
                        Log.d("MyApp", "User signed up and logged in through Facebook!");
                        showUserProfile();
                    } else {
                        Log.d("MyApp", "User logged in through Facebook!");
                        showUserProfile();
                    }
                }
            });
        }
    });

    return rootView;
}

private void createFacebookUser() {
    GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                    if (jsonObject != null) {
                        JSONObject userProfile = new JSONObject();

                        try {
                            user.put("facebookId", jsonObject.getLong("id"));
                            user.setUsername(jsonObject.getString("name"));

                            if (jsonObject.getString("email") != null)
                                user.setEmail(jsonObject.getString("email"));

                            user.signUpInBackground(new SignUpCallback() {
                                public void done(ParseException e) {
                                    if (e == null) {
                                        // Fragmentwechsel
                                        final FragmentTransaction ft = getFragmentManager().beginTransaction();
                                        ft.replace(R.id.container, new Welcome(), "NewFragmentTag");
                                        ft.commit();
                                        // Infoanzeige, dass Anmeldung erfolgreich war
                                        Toast.makeText(getActivity().getApplicationContext(),
                                                getResources().getString(R.string.signup_success),
                                                Toast.LENGTH_LONG).show();

                                    } else {
                                        Toast.makeText(getActivity().getApplicationContext(),
                                                getResources().getString(R.string.error),
                                                Toast.LENGTH_LONG).show();
                                    }
                                }
                            });

                            // Show the user info
                        } catch (JSONException e) {
                            Log.d(ParseApplication.TAG,
                                    "Error parsing returned user data. " + e);
                        }
                    } else if (graphResponse.getError() != null) {
                        switch (graphResponse.getError().getCategory()) {
                            case LOGIN_RECOVERABLE:
                                Log.d(ParseApplication.TAG,
                                        "Authentication error: " + graphResponse.getError());
                                break;

                            case TRANSIENT:
                                Log.d(ParseApplication.TAG,
                                        "Transient error. Try again. " + graphResponse.getError());
                                break;

                            case OTHER:
                                Log.d(ParseApplication.TAG,
                                        "Some other error: " + graphResponse.getError());
                                break;
                        }
                    }
                }
            });
    request.executeAsync();
}

private void showUserProfile(){
    final FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.container, new TestProfile(), "NewFragmentTag");
    ft.addToBackStack(null);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
    }
}

我还做了facebook和解析指南建议的所有内容来设置登录。

任何人都可以帮助我吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

添加此行

ParseFacebookUtils.initialize(context);
onCreateView

并尝试

Read This Documentation carefully