何时使用 Facebook 的新 Android SDK 3.0 请求权限? [英] When to request permissions with Facebook's new Android SDK 3.0?

查看:19
本文介绍了何时使用 Facebook 的新 Android SDK 3.0 请求权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着 Facebook 的新 Android SDK 3.0(几天前发布),身份验证过程发生了变化.

With Facebook's new Android SDK 3.0 (that was released a few days ago), the process of authentication has changed.

那么你如何请求诸如friends_hometown"之类的读取权限?

So how do you request a read permission such as "friends_hometown"?

以下代码是我正在尝试这样做的方式 - 但我很确定这不是您应该这样做的方式:

The following code is how I am trying to do it - but I'm quite sure it's not the way you should do this:

版本 1:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Session.openActiveSession(this, true, new Session.StatusCallback() { // start Facebook login
        @Override
        public void call(Session session, SessionState state, Exception exception) { // callback for session state changes
            if (session.isOpened()) {
                List<String> permissions = new ArrayList<String>();
                permissions.add("friends_hometown");
                session.requestNewReadPermissions(new Session.NewPermissionsRequest(FBImport.this, permissions));
                Request.executeGraphPathRequestAsync(session, "me/friends/?access_token="+session.getAccessToken()+"&fields=id,name,hometown", new Request.Callback() {
                    ...
                });
            }
        }
    });
}

版本 2:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Session currentSession = Session.getActiveSession();
    if (currentSession == null || currentSession.getState().isClosed()) {
        Session session = Session.openActiveSession(this, true, fbStatusCallback); // PROBLEM: NO PERMISSIONS YET BUT CALLBACK IS EXECUTED ON OPEN
        currentSession = session;
    }
    if (currentSession != null && !currentSession.isOpened()) {
        OpenRequest openRequest = new OpenRequest(this).setCallback(fbStatusCallback); // HERE IT IS OKAY TO EXECUTE THE CALLBACK BECAUSE WE'VE GOT THE PERMISSIONS
        if (openRequest != null) {
            openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
            openRequest.setPermissions(Arrays.asList("friends_hometown"));
            openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
            currentSession.openForRead(openRequest);
        }
    }
}

我正在做的是在会话打开后立即请求权限 - 但此时代码已经开始 Graph API 请求,因此权限请求来晚了......

What I'm doing is to request the permission as soon as the session is open - but at this point the code is already starting a Graph API request, thus the permission request comes to late ...

不能在初始化会话的同时请求权限吗?

Can't you request a permission at the same time you initialize the session?

推荐答案

我建议您阅读我们的登录教程 这里 特别是在第 3 步中.使用我们提供的登录按钮是最方便的方法,(参见 authButton.setReadPermissions())

I recommend that you read our login tutorial here specifically in step 3. Using the login button that we provide is the most convenient method, (see authButton.setReadPermissions())

在不使用登录按钮的情况下设置权限比较棘手,因为您必须手动完成所有会话管理.深入研究登录按钮的源代码,这个 代码行 可能是您所需要的.看起来您需要创建自己的 Session.OpenRequest 并设置它的属性,例如权限、受众和登录行为,然后获取当前会话并调用 openForRead() 在您的 Session.OpenRequest 上.

To set permissions without using the loginbutton is trickier since you will have to do all the session management by hand. Digging into the source code for the login button, this line of code is probably what you need. It looks like you will need to create your own Session.OpenRequest and set it's attributes such as permissions, audience, and login behavior, then get the current session and call openForRead() on your Session.OpenRequest.

这篇关于何时使用 Facebook 的新 Android SDK 3.0 请求权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆