如何在Facebook的集成中获得社交认证中的唯一ID和个人资料信息

时间:2015-03-24 08:51:26

标签: android integration socialauth

我已经实现了社交认证,用于整合facebook现在我想获取用户个人资料信息和唯一ID。我已获得访问令牌但无法获取用户个人资料信息。我已通过以下方式实现     公共类SignUp extends Activity实现OnClickListener {     private EditText email_mEditText,password_mEditText;

SocialAuthAdapter socialAuthAdapter;
private RelativeLayout facebook_layout, twitter_layout, googleplus_layout, linkdin_layout;
private Button signIn_mButton;
String email_mString, password_mString;
private ImageView backIcon_mImageView, logoIcon_mImageView;
private TextView forgotpassword_mTextView;
 String emailid_mString ;
 EditText emailid_mEt;
 Button submit_mbtn;
 Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sign_up);
    signIn_mButton = (Button) findViewById(R.id.signin_button);
    email_mEditText = (EditText) findViewById(R.id.email_editText);
    password_mEditText = (EditText) findViewById(R.id.password_editText);
    backIcon_mImageView = (ImageView) findViewById(R.id.backicon_imageView);
    logoIcon_mImageView = (ImageView) findViewById(R.id.logoicon_imageView);

    facebook_layout = (RelativeLayout) findViewById(R.id.facebook_layout);
    twitter_layout = (RelativeLayout) findViewById(R.id.twitter_layout);
    googleplus_layout = (RelativeLayout) findViewById(R.id.google_layout);
    linkdin_layout = (RelativeLayout) findViewById(R.id.linkdin_layout);
    facebook_layout.setOnClickListener(this);
    twitter_layout.setOnClickListener(this);
    googleplus_layout.setOnClickListener(this);
    linkdin_layout.setOnClickListener(this);

    socialAuthAdapter = new SocialAuthAdapter(new ResponseListener());
    signIn_mButton.setOnClickListener(this);
    backIcon_mImageView.setOnClickListener(this);
    logoIcon_mImageView.setOnClickListener(this);
    forgotpassword_mTextView = (TextView) findViewById(R.id.forgotpassword_textView);
    forgotpassword_mTextView.setPaintFlags(forgotpassword_mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    forgotpassword_mTextView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.signin_button:

        email_mString = email_mEditText.getText().toString();
        password_mString = password_mEditText.getText().toString();
        if ((email_mString.isEmpty()) && (password_mString.isEmpty())) {
            email_mEditText.addTextChangedListener(new TextWatcher() {
                @Override
                public void afterTextChanged(Editable s) {

                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub
                     email_mEditText.setError("Value required");
                }

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub
                     email_mEditText.setError(null);
                }
            });


        } else if ((email_mString.isEmpty())) {
            email_mEditText.setError("Please Enter the Email ");

        } else if ((password_mString.isEmpty())) {
             password_mEditText.setError("Please Enter the Password ");
        } else {

            Log.d("u r", "ur in else part");
            new Thread(null, threadEventListing, "").start();

        }
        break;
    case R.id.backicon_imageView:
        Intent signInIntent = new Intent(SignUp.this, SignIn.class);
        startActivity(signInIntent);
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
        finish();
        break;
    case R.id.logoicon_imageView:
        Intent signIntent = new Intent(SignUp.this, SignIn.class);
        startActivity(signIntent);
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
        finish();
        break;
    case R.id.forgotpassword_textView:

        Log.d("hello", "u clicked FOrgot textview");
        dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.forgot_password);
        emailid_mEt=(EditText)dialog.findViewById(R.id.etforgotpassword);
        submit_mbtn=(Button)dialog.findViewById(R.id.btnsubmit);
        submit_mbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                emailid_mString = emailid_mEt.getText().toString();
                new Thread(null,threadForgotPassword,"").start();
            }
        });
        dialog.show();

        break;
    case R.id.facebook_layout:

        socialAuthAdapter.authorize(SignUp.this, Provider.FACEBOOK);
        socialAuthAdapter.signOut(this, Provider.FACEBOOK.toString());

        break;

    case R.id.twitter_layout:

        socialAuthAdapter.authorize(SignUp.this, Provider.TWITTER);
        socialAuthAdapter.signOut(this, Provider.TWITTER.toString());

        break;

    case R.id.google_layout:
        socialAuthAdapter.authorize(SignUp.this, Provider.GOOGLEPLUS);
        socialAuthAdapter.signOut(this, Provider.GOOGLEPLUS.toString());

        break;

    case R.id.linkdin_layout:
        socialAuthAdapter.authorize(SignUp.this, Provider.LINKEDIN);
        socialAuthAdapter.signOut(this, Provider.LINKEDIN.toString());
        break;
    }

}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    Intent signInIntent = new Intent(SignUp.this, SignIn.class);
    startActivity(signInIntent);
    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
    finish();
}

private final class ResponseListener implements DialogListener {
    public void onComplete(Bundle values) {
        Log.d("tag", "In on comppleter");

        String token = socialAuthAdapter.getCurrentProvider().getAccessGrant().getKey();
        Log.d("Token", token);

    }

    @Override
    public void onBack() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCancel() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onError(SocialAuthError arg0) {
        // TODO Auto-generated method stub

    }

}

}

2 个答案:

答案 0 :(得分:0)

使用此代码

public class MainFragment extends Fragment {

    private static final String TAG = "MainFragment";
    private UiLifecycleHelper uiHelper;

    private Session.StatusCallback callback = new Session.StatusCallback() {

        @Override
        public void call(Session session, SessionState state, Exception exception) {

            onSessionStateChange(session, state, exception);
        }
    };

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

        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);

        View view = inflater.inflate(R.layout.main, container, false);

        LoginButton authButton = (LoginButton)  view.findViewById(R.id.authButton);
        authButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_location"));

        authButton.setFragment(this);
        return view;
    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {

        if (state.isOpened()) {

            Log.i(TAG, "Logged in...");
            makeMeRequest(session);
        } else if (state.isClosed()) {

            Log.i(TAG, "Logged out...");
        }
    }

    private void makeMeRequest(final Session session) {

        // Make an API call to get user data and define a
        // new callback to handle the response.
        Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {

            /*
             * @Override public void onCompleted(GraphUser user, Response
             * response) { // If the response is successful if (session ==
             * Session.getActiveSession()) { if (user != null) { // Set the
             * id for the ProfilePictureView // view that in turn displays
             * the profile picture. Log.e("user",user.toString()); } } if
             * (response.getError() != null) { // Handle errors, will do so
             * later. } }
             */

            @Override
            public void onCompleted(GraphUser user, Response response) {

                // TODO Auto-generated method stub
                Log.e("FB Return", user.toString());
                Log.e("FullName", user.getName());
                Log.e("Id", user.getId());
                Log.e("username", "" + user.getUsername());
                Log.e("Fname", user.getFirstName());
                Log.e("Fname", user.getLastName());
                Log.e("gender", user.asMap().get("gender").toString());

                String gender = user.asMap().get("gender").toString();
                String email = (String) user.asMap().get("email") + "";
                Log.e("email", email);
            }
        });
        request.executeAsync();
    }

    @Override
    public void onResume() {

        super.onResume();
        // For scenarios where the main activity is launched and user
        // session is not null, the session state change notification
        // may not be triggered. Trigger it if it's open/closed.
        Session session = Session.getActiveSession();
        if (session != null && (session.isOpened() || session.isClosed())) {

            onSessionStateChange(session, session.getState(), null);
        }
        uiHelper.onResume();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onPause() {

        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }
}

答案 1 :(得分:0)

由于您使用的是社交身份验证,因此您可以尝试:

private final class ProfileDataListener implements SocialAuthListener {

@Override
public void onExecute(Profile t) {

  Log.d("Custom-UI", "Receiving Data");
  Profile profileMap = t;
  Log.d("Custom-UI",  "Validate ID = " + profileMap.getValidatedId());
  Log.d("Custom-UI",  "First Name  = " + profileMap.getFirstName());
  Log.d("Custom-UI",  "Last Name  = " + profileMap.getLastName());
  Log.d("Custom-UI",  "Email  = " + profileMap.getEmail());
  Log.d("Custom-UI",  "Gender  = " + profileMap.getGender());
  Log.d("Custom-UI",  "Country  = " + profileMap.getCountry());
  Log.d("Custom-UI",  "Language  = " + profileMap.getLanguage());
  Log.d("Custom-UI",  "Location  = " + profileMap.getLocation());
  Log.d("Custom-UI",  "Profile Image URL = " + profileMap.getProfileImageURL()); }