gpt4 book ai didi

android - facebook android sdk - 用户电子邮件返回空

转载 作者:太空狗 更新时间:2023-10-29 16:19:09 25 4
gpt4 key购买 nike

我有以下代码让用户通过我的应用程序登录到 facebook。我第一次将 user.getproperty("email") 设置为 null。当我第二次运行该应用程序时,我得到了值(value)。下面是我的代码。

私有(private)无效 loginToFb() {

    final Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
LandingPageActivity.this, Arrays.asList(
"user_location", "user_birthday",
"user_likes", "email"));
Session.openActiveSession(this, true, new Session.StatusCallback() {

// callback when session changes state
@Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
session.requestNewReadPermissions(newPermissionsRequest);
// make request to the /me API
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {

// callback after Graph API response with user
// object
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
fetchUserFbDetails(user);
}
}

});
Request.executeBatchAsync(request);

}

}
});

}

最佳答案

您不应自己管理 session ,而应使用 Facebook 的 LoginButtonUiLifecycleHelper

1) 在您的 UI 中声明一个 LoginButton:

<!-- FACEBOOK LOGIN -->
<com.facebook.widget.LoginButton
android:id="@+id/facebookLoginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />


2) 创建一个 LoginFragment 并声明一个私有(private)成员:

private UiLifecycleHelper mFacebookLifecycleHelper;


3)在Fragment的onCreate()方法中,初始化LoginButton并添加一个钩子(Hook)来获取用户信息:

LoginButton facebookLoginButton = (LoginButton) view.findViewById(R.id.facebookLoginBtn);
facebookLoginButton.setFragment(this);

// Intercept the facebook user returned from login
facebookLoginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {

@Override
public void onUserInfoFetched(GraphUser user) {
mFacebookUser = user;

if (user != null) {
LogUtils.LOGFB(TAG, "Got a Facebook user: " + user.getFirstName() +
" " + user.getLastName() + ", email: " + user.getProperty("email"));
}
else {
LogUtils.LOGFB(TAG, "No Facebook user");
}
}
});

// Set extra read permissions
facebookLoginButton.setReadPermissions(Arrays.asList(
"email", "user_birthday", ...));


4) 创建回调以监听 Facebook session 状态的变化:

// Listen for changes in Facebook session state
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};


// Respond to Facebook session state changes
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
LogUtils.LOGFB(TAG, "Logged in using facebook");
LogUtils.LOGFB(TAG, "Access Token: " + session.getAccessToken());
// TODO: display authenticated UI
} else if (state.isClosed()) {
LogUtils.LOGFB(TAG, "Logged out from facebook");
// TODO: display the non-authenticated UI
}
}


5) 然后确保为 fragment 的所有生命周期更改调用 lifecycleHelper:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Creates the Facebook session and opens it automatically if a cached token is available.
mFacebookLifecycleHelper = new UiLifecycleHelper(getActivity(), callback);
mFacebookLifecycleHelper.onCreate(savedInstanceState);
...
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mFacebookLifecycleHelper.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onResume() {
super.onResume();
mFacebookLifecycleHelper.onResume();
}
@Override
public void onPause() {
super.onPause();
mFacebookLifecycleHelper.onPause();
}

@Override
public void onDestroy() {
mFacebookLifecycleHelper.onDestroy();
super.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mFacebookLifecycleHelper.onSaveInstanceState(outState);
}

关于android - facebook android sdk - 用户电子邮件返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19797770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com