1

我已经在我的应用程序中实现了 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")));?在我的代码中,我搜索了很多并查看了很多答案,但我仍然对如何在我的代码中实现它感到困惑。

4

2 回答 2

4

以下对我来说就像一个魅力。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mLogin = (Button) findViewById(R.id.logIn);
    mLogin.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    Session currentSession = Session.getActiveSession();
    if (currentSession == null || currentSession.getState().isClosed()) {
        Session session = new Session.Builder(context).build();
        Session.setActiveSession(session);
        currentSession = session;
    }

    if (currentSession.isOpened()) {
        // Do whatever you want. The user is logged in.

    }
    else
        if (!currentSession.isOpened()) {
            // Ask for username and password
            OpenRequest op = new Session.OpenRequest((Activity) context);

            op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
            op.setCallback(null);

            List<String> permissions = new ArrayList<String>();
            permissions.add("publish_stream");
            permissions.add("user_likes");
            permissions.add("email");
            permissions.add("user_birthday");
            op.setPermissions(permissions);

            Session session = new Builder(MainActivity.this).build();
            Session.setActiveSession(session);
            session.openForPublish(op);
        }
}

public void call(Session session, SessionState state, Exception exception) {

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (Session.getActiveSession() != null)
        Session.getActiveSession().onActivityResult(this, requestCode,
                                                    resultCode, data);

    Session currentSession = Session.getActiveSession();
    if (currentSession == null || currentSession.getState().isClosed()) {
        Session session = new Session.Builder(context).build();
        Session.setActiveSession(session);
        currentSession = session;
    }

    if (currentSession.isOpened()) {
        Session.openActiveSession(this, true, new Session.StatusCallback() {

            @Override
            public void call(final Session session, SessionState state,
                             Exception exception) {

                if (session.isOpened()) {
                    Request.executeMeRequestAsync(
                        session,
                        new Request.GraphUserCallback() {

                            @Override
                            public void onCompleted(GraphUser user,
                                    Response response) {
                                if (user != null) {
                                    TextView welcome = (TextView) findViewById(R.id.welcome);
                                    welcome.setText("Hello "
                                            + user.getName() + "!");

                                    access_token = session
                                            .getAccessToken();
                                    firstName = user.getFirstName();
                                    fb_user_id = user.getId();

                                    System.out
                                            .println("Facebook Access token: "
                                                    + access_token);
                                    System.out.println("First Name:"
                                            + firstName);
                                    System.out.println("FB USER ID: "
                                            + fb_user_id);
                                }
                            }
                        });
                }
            }
        });
    }
}
于 2013-05-03T10:39:22.923 回答
3

您还需要在developers.facebook.com 中设置“电子邮件”权限。

应用程序>定义>权限>用户和朋友权限>(写)“电子邮件”

于 2013-05-02T10:13:02.030 回答