如何使用 Facebook SDK 4 获取姓名和电子邮件?
How Can I Fetch Name and Email using Facebook SDK 4?
我正在使用 Facebook SDK 4 ,我在 sdk 2 之前已经这样做了...但是对我来说一些语法已经改变所以我遇到了一些问题...我'我无法获取用户名、电子邮件
这是我的 XML 文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.login.widget.LoginButton
android:id="@+id/LoginFbButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Login With Facebook" />
</LinearLayout>
这是我的 Facebook 登录代码
private void loginToFacebook() {
Session.openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Log.i(TAG, "Access Token" + session.getAccessToken());
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
try {
firstName=user.getFirstName();
lastName=user.getLastName();
Log.d("Facebook User", "details:" + firstName+""+lastName);
}catch(Exception e){
查看我的 Logcat
com.facebook.katana.platform.PlatformService has leaked ServiceConnection com.facebook.fbservice.ops.DefaultBlueServiceOperationFactory$DefaultOperation$BlueServiceConnection@4307e1d8 that was originally bound here E/ActivityThread(28413): android.app.ServiceConnectionLeaked: Service com.facebook.katana.platform.PlatformService has leaked ServiceConnection com.facebook.fbservice.ops.DefaultBlueServiceOperationFactory$DefaultOperation$BlueServiceConnection@4307e1d8 that was originally bound here
如果我理解v4,你首先需要获得令牌,然后解决你想要的所有查询,我个人这样做:
mLoginFacebookButton.setFragment(this);
mLoginFacebookButton.setReadPermissions(Arrays.asList("email"));
mLoginFacebookButton.registerCallback(mCallbackManager, callback);
回调为:
FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject user, GraphResponse response) {
email = user.getString("email");
firstName = user.getString("first_name");
lastName = user.getString("last_name");
}
}).executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
};
您可以在此
中找到有关您儿子的详细信息
另请参阅 Facebook 开发者 notes
就做这样的事
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
pDialog.dismiss();
Log.d("Response",response.getJSONObject().toString());
if (response.getError() != null) {
// handle error
} else {
String email = object.optString("email");
String fname = object.optString("fname");
String lname = object.optString("lname");
Log.d("Email",email);
Log.d("fname",fname);
Log.d("lname",lname);
// Log.d("Response", response.getInnerJsobject.toString());
}
}
});
我正在使用 Facebook SDK 4 ,我在 sdk 2 之前已经这样做了...但是对我来说一些语法已经改变所以我遇到了一些问题...我'我无法获取用户名、电子邮件
这是我的 XML 文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.login.widget.LoginButton
android:id="@+id/LoginFbButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Login With Facebook" />
</LinearLayout>
这是我的 Facebook 登录代码
private void loginToFacebook() {
Session.openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Log.i(TAG, "Access Token" + session.getAccessToken());
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
try {
firstName=user.getFirstName();
lastName=user.getLastName();
Log.d("Facebook User", "details:" + firstName+""+lastName);
}catch(Exception e){
查看我的 Logcat
com.facebook.katana.platform.PlatformService has leaked ServiceConnection com.facebook.fbservice.ops.DefaultBlueServiceOperationFactory$DefaultOperation$BlueServiceConnection@4307e1d8 that was originally bound here E/ActivityThread(28413): android.app.ServiceConnectionLeaked: Service com.facebook.katana.platform.PlatformService has leaked ServiceConnection com.facebook.fbservice.ops.DefaultBlueServiceOperationFactory$DefaultOperation$BlueServiceConnection@4307e1d8 that was originally bound here
如果我理解v4,你首先需要获得令牌,然后解决你想要的所有查询,我个人这样做:
mLoginFacebookButton.setFragment(this);
mLoginFacebookButton.setReadPermissions(Arrays.asList("email"));
mLoginFacebookButton.registerCallback(mCallbackManager, callback);
回调为:
FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject user, GraphResponse response) {
email = user.getString("email");
firstName = user.getString("first_name");
lastName = user.getString("last_name");
}
}).executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
};
您可以在此
另请参阅 Facebook 开发者 notes
就做这样的事
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
pDialog.dismiss();
Log.d("Response",response.getJSONObject().toString());
if (response.getError() != null) {
// handle error
} else {
String email = object.optString("email");
String fname = object.optString("fname");
String lname = object.optString("lname");
Log.d("Email",email);
Log.d("fname",fname);
Log.d("lname",lname);
// Log.d("Response", response.getInnerJsobject.toString());
}
}
});