Facebook SDK 3.7 for Android请求电子邮件权限

时间:2014-02-27 18:20:29

标签: android facebook email facebook-android-sdk facebook-sdk-3.0

我使用Facebook SDK for Android允许用户注册和使用应用。我想获得一些关于用户的基本信息,如用户名和电子邮件。无论如何,我有问题,因为我在尝试获取用户电子邮件时遇到空指针异常。

String email = user.getProperty("email").toString();

我假设我收到此错误,因为我没有请求电子邮件权限。我怎么能这样做?

以下是完整代码

public class RegisterActivity extends ActionBarActivity{

Button btnFacebook;

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

    getSupportActionBar().hide();

    btnFacebook = (Button) findViewById(R.id.btn_facebook);


    btnFacebook.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Log.d("FACEBOOK", "Prije sesije");
            // start Facebook Login
                        Session.openActiveSession(RegisterActivity.this, true, new Session.StatusCallback() {
                            // callback when session changes state
                            @SuppressWarnings("deprecation")
                            @Override
                            public void call(Session session, SessionState state,
                                    Exception exception) {
                                Log.d("FACEBOOK", "U call");
                                if(session.isOpened()){                     

                                    Log.d("FACEBOOK", "ako je sesija otvorena");

                                    // make request to the /me API
                                    Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                                        @Override
                                        public void onCompleted(GraphUser user, Response response) {
                                            Log.d("FACEBOOK", "onCompleted");

                                            if(user != null){
                                                Log.d("FACEBOOK", "user razlicit od null");
                                                 String email = user.getProperty("email").toString();
                                                Toast.makeText(getApplicationContext(), "Ime "+user.getUsername().toString()+ "Email "+ email , 
                                                        Toast.LENGTH_SHORT).show();

                                                String userId = user.getId();


                                                //new DownloadImageTask(ivProfile).execute("http://graph.facebook.com/"+userId+"/picture?type=large");



                                                Log.d("ID", userId);



                                            }
                                            Log.d("ID", user.getId());


                                        }
                                    });
                                }
                                Log.d("FACEBOOK", "nije u sesisji");


                            }

                        });

        }
    });

}

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

2 个答案:

答案 0 :(得分:10)

首先获取权限

private static final List<String> PERMISSIONS = Arrays.asList(
        "email","user_location");

获取用户信息的方法

      Session.openActiveSession(RegisterActivity.this, true, new Session.StatusCallback() {

        // callback when session changes state
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            /*
             * Session.getActiveSession().onActivityResult(activity1,64206,
             * -1,activity1.getIntent());
             */

            if (session.isOpened()) {

                List<String> permissions = session.getPermissions();
                if (!isSubsetOf(PERMISSIONS, permissions)) {
                    Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
                            RegisterActivity.this, PERMISSIONS);
                    session.requestNewReadPermissions(newPermissionsRequest);
                    return;
                }
                // make request to the /me API
                Request.newMeRequest(session, new GraphUserCallback() {

                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        // TODO Auto-generated method stub
                        Log.d("",""+user);  
                        try {

                            name=user.getName();
                            email=user.getProperty("email").toString();
                            location=(user.getLocation().getProperty("name").toString());

                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                }).executeAsync();

            }
        }
    });

isSubsetof方法

private boolean isSubsetOf(Collection<String> subset,
        Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}

将此代码放在 onActivityResult

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.d("Activity result","  "+requestCode+"   "+resultCode+"    "+data.toString());
    Session.getActiveSession().onActivityResult(this, requestCode,
            resultCode, data);
}

Internet 权限置于Manifest

   <uses-permission android:name="android.permission.INTERNET" />

在清单

中声明 Facebook登录活动

        

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

答案 1 :(得分:0)

嘿,你可以这样试试......

session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("id", "name", "picture","installed" )).setCallback(statusCallback));