我已经在我的应用程序中实现了 Facebook SDK,但我没有收到用户电子邮件(我认为我需要设置获取用户电子邮件的权限 - 根据Fetch User Data),但不知何故它对我不起作用。我应该如何改进我的代码来完成它?
这是我的代码。
登录界面.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.facebook.*;
import com.facebook.Session.OpenRequest;
import com.facebook.model.*;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class LoginScreen extends Activity{
public static final String[] PERMISSIONS = new String[] {"email"};
public String fbId,first_name,last_name,email;
private Button button_fb;
JSONObject result;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.loginhome);
button_fb = (Button) findViewById(R.id.btn_signin_fb);
button_fb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
// start Facebook Login
Session.openActiveSession(LoginScreen.this, true, new Session.StatusCallback() {
// session.openForRead(new OpenRequest(LoginScreen.this).setPermissions(Arrays.asList("email")));
// Callback when session changes state
@Override
public void call(final Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// Make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// Callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
GraphObject graphObject = response.getGraphObject();
Log.d("graphboject",""+graphObject);
if (user != null) {
Toast.makeText(getApplicationContext(), user.getName(), Toast.LENGTH_SHORT).show();
fbId = user.getId();
first_name = user.getFirstName();
last_name = user.getLastName();
result = user.getInnerJSONObject();
email = (String) user.getProperty("email");
String emal = (String) response.getGraphObject().getProperty("email");
// String em = (String) user.asMap().get("email");
Log.d("user email",
(String) user.getProperty("email") + "link"
+ user.getLink());
Log.d("Fb fb_id", fbId);
Log.d("Fb first name", first_name);
Log.d("Fb last name", last_name);
Log.d("FB email", emal);
Log.d("Fb json", String.valueOf(result));
// Log.d("Fb email", email);
}
}
});
}
}
});
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}
我检查了我的控制台。我得到了一切,比如用户名、名字、姓氏、fb_id,但我没有收到用户的电子邮件。
我该怎么做?
我应该如何使用session.openForRead(new OpenRequest(LoginScreen.this).setPermissions(Arrays.asList("email")));
?在我的代码中,我搜索了很多并查看了很多答案,但我仍然对如何在我的代码中实现它感到困惑。