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

Posted

技术标签:

【中文标题】Facebook SDK 3.7 for Android 请求电子邮件权限【英文标题】:Facebook SDK 3.7 for Android request email permission 【发布时间】:2014-02-27 18:20:28 【问题描述】:

我正在使用 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);
  

【问题讨论】:

【参考方案1】:

先获取权限

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);

在 Manifest 中添加 Internet 权限

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

在清单中声明 Facebook 登录活动

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

【讨论】:

当我如下添加了一个权限(生日)时,它不起作用。多次加载 RegisterActivity。 私有静态最终列表 PERMISSIONS = Arrays.asList("email","user_location","birthday"); 嗨,我也遇到了同样的问题。您的回答很好,但 facebook 首先要求基本权限,如果我接受它们,弹出窗口会再次打开并要求邮件许可。知道如何只有一个弹出窗口来询问邮件权限吗?谢谢 :) @Maxime 不,你不能,使用 Facebook 3.0 在登录期间你不能请求超过默认提供的 public_profile 权限。 这个解决方案很完美【参考方案2】:

你可以试试这个方法...

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

【讨论】:

以上是关于Facebook SDK 3.7 for Android 请求电子邮件权限的主要内容,如果未能解决你的问题,请参考以下文章

Facebook SDK for iOS 处理登录

Facebook SDK Initialization for SDK v3.23.1 为 Android 实现应用事件

Facebook Sdk for ios 发布 - 暗屏

通过 FB SDK for iOS 将视频分享到 facebook

FaceBook SDK for Unity 有谁用过吗,请问pc端程

有没有办法知道用户使用 facebook SDK for Android 在 facebook 上有蓝色勾号?