我已在我的应用中集成了facebook登录功能。它工作正常,在我得到的回调方法
这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_login);
findViewById(R.id.sign_in_button).setOnClickListener(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("basic_info","user_friends","email");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.e("res",""+loginResult.getAccessToken());
Toast.makeText(LoginActivity.this,""+loginResult.getAccessToken().getUserId(),Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
}
当我打印访问令牌时,我得到了
{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[user_friends, contact_email, email, public_profile, basic_info]}
我一直在寻找如何从FB PROFILE DATA
获取用户的数据方法
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();
但它要求一个看法。我从哪里开会?
答案 0 :(得分:1)
登录后,您可以使用Profile类。
Profile.getCurrentProfile() //get current instance of logged profile
Profile.getCurrentProfile().getId() //get current id
Profile.getCurrentProfile().getName() //get current user name
有关详情,请参阅documentation。
答案 1 :(得分:0)
以下是包含状态回叫和会话处理的完整代码..
public class LoginFragment extends Fragment {
private LoginButton facebookButton;
private UiLifecycleHelper uiHelper;
//use this
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
try {
// 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())) {
if (pDialog != null) {
onSessionStateChange(session, session.getState(), null);
}
}
uiHelper.onResume();
} catch (Exception e) {
Log.e(Constants.TAG_EXCEPTION, e.toString());
}
}
@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);
}
public void showLoggedInUserInfo(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) {
fbFirstName = user.getFirstName();
fbLastName = user.getLastName();
fbEmail = user.asMap().get("email").toString();
}
}
}
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (state.isOpened()) {
showLoggedInUserInfo(session);
} else if (state.isClosed()) {
// Logged out
}
}
});
request.executeAsync();
}