我目前正在开发一个旨在通过Facebook帐户验证任何用户的应用程序。
我无法从用户的帐户收到用户电子邮件。我的代码在
之下 private void signInWithFacebook() {
SessionTracker mSessionTracker = new SessionTracker(getBaseContext(), new StatusCallback() {
public void call(Session session, SessionState state, Exception exception) {
}
}, null, false);
String applicationId = Utility.getMetadataApplicationId(getBaseContext());
Session mCurrentSession = mSessionTracker.getSession();
if (mCurrentSession == null || mCurrentSession.getState().isClosed()) {
mSessionTracker.setSession(null);
Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
Session.setActiveSession(session);
mCurrentSession = session;
}
if (!mCurrentSession.isOpened()) {
Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(FacebookLoginActivity.this);
if (openRequest != null) {
openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
openRequest.setPermissions(Arrays.asList("user_birthday", "email", "user_location"));
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
mCurrentSession.openForRead(openRequest);
}
}else {
Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
public void onCompleted(GraphUser user, Response response) {
Log.w("myConsultant", user.getId() + " " + user.getName() + " " + user.getLink() + " "+ response);
}
});
}
}
我正在使用适用于Android的Facebook SDK 3.0.1。 我已经设置了Facebook Graph Api所需的权限。 在响应xml中,没有像电子邮件这样的字段。 Facebook Sdk文档不够好,我不知道如何获取电子邮件地址。
非常感谢您的及时回复。
提前致谢,
答案 0 :(得分:14)
电子邮件不是返回的默认字段。相反,您应该创建一个meRequest,并传递一个参数,如:fields = email。
Request me = Request.newMeRequest(mCurrentSession, new GraphRequestCallback() {...});
Bundle params = me.getParameters();
params.putString("fields", "email,name");
me.setParameters(params);
me.executeAsync();
答案 1 :(得分:4)
你在常规事物中就这样打电话 - 只有不同的你不能直接user.email
的System.out.println(user.asMap()得到( “电子邮件”)的toString());
常规:
@Override
public void onClick(View v) {
// start Facebook Login
Session.openActiveSession(Giris.this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(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) {
if (user != null) {
System.out.println(user.getName());
System.out.println(user.getBirthday());
System.out.println(user.getFirstName());
System.out.println(user.getLastName());
System.out.println(user.getLink());
System.out.println(user.getUsername());
System.out.println(user.getLocation());
System.out.println("facebook user id" + user.getId());
System.out.println(user.asMap().get("email").toString());
// Session.OpenRequest open = new Session.OpenRequest(Login)
}
}
});
}
}
});
}
答案 2 :(得分:1)
只需调用此方法即可将用户电子邮件ID返回给您。
private void doSocialNetworkinWithFacebook()
{
// check for session
Session session=Session.getActiveSession();
if (session != null && session.isOpened())
{
// user is already login show
try
{
Session.OpenRequest request = new Session.OpenRequest(this);
request.setPermissions(Arrays.asList("email", "publish_actions"));
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Request.executeMeRequestAsync(session, new Request.GraphUserCallback()
{
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response)
{
if (user != null)
{
Toast.makeText(activity, "Welcome "+user.getName(), Toast.LENGTH_SHORT).show();
// publishFeedDialog(session);
try
{
strFirstName = user.getFirstName().toString();
strLocation = user.getLocation().getProperty("name").toString();
strEmail = user.asMap().get("email").toString();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
strEmail="";
}
runOnUiThread(new Runnable()
{
public void run()
{
setUserInfoFromFacebook(strFirstName, strLocation, strEmail);
}
});
}
}
});
}
else
{
// user is not log in
//show login screen
// start Facebook Login
try
{
Session.OpenRequest request = new Session.OpenRequest(this);
request.setPermissions(Arrays.asList("email", "publish_actions"));
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Session.openActiveSession(activity, true, new Session.StatusCallback()
{
// callback when session changes state
@Override
public void call(final Session session, SessionState state, Exception exception)
{
//session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("email")));
Log.d(TAG, "Session :"+session.toString());
Log.d(TAG, "Session is opened :"+session.isOpened());
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)
{
if (user != null)
{
Toast.makeText(activity, "Welcome "+user.getName(), Toast.LENGTH_SHORT).show();
// publishFeedDialog(session);
try
{
strFirstName = user.getFirstName().toString();
strLocation = user.getLocation().getProperty("name").toString();
strEmail = user.asMap().get("email").toString();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
strEmail="";
}
runOnUiThread(new Runnable()
{
public void run()
{
setUserInfoFromFacebook(strFirstName, strLocation, strEmail);
}
});
}
}
});
}
else if(session.isClosed())
{
Toast.makeText(activity, "Unable to connect facebook, please try later..",Toast.LENGTH_SHORT).show();
}
}
});
}
}
答案 3 :(得分:0)
首先添加相应的权限,然后使用getProperty
功能获取电子邮件。
permissions = Arrays.asList("email","languages","user_location","user_likes", "user_education_history","user_work_history","user_hometown","user_about_me","user_status");
Log.i(TAG, "user_email : " + user.getProperty("email"));