当请求与Facebook的新的Andr​​oid SDK 3.0的权限? [英] When to request permissions with Facebook's new Android SDK 3.0?

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

问题描述

使用Facebook的新的Andr​​oid 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"?

下面code是如何我试图做到这一点 - 但我敢肯定它不是你应该做这样的方式:

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

我在做什么是尽快申请许可的会议是开放的 - 但在这一点上code已经开始一个图形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?

推荐答案

我建议你仔细阅读我们的登录教程<一href="https://developers.facebook.com/docs/howtos/androidsdk/3.0/login-with-facebook/#step3">here特别是在第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())

编辑:

要设置权限,而无需使用loginbutton是棘手的,因为你将不得不用手来完成所有的会话管理。挖掘到源$ C ​​$ C登录按钮,这<一href="https://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/com/facebook/widget/LoginButton.java#L634">line $了C $ C 可能是你所需要的。它看起来像您将需要创建自己的 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的新的Andr​​oid SDK 3.0的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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