问题描述
所以你如何请求这样的读取权限,如Facebook的新的Android SDK 3.0(发布前几天),认证过程已经改变。作为friends_hometown?
以下代码是我如何尝试这样做的 - 但我确信这不是你应该这样做的:
版本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){//回调会话状态更改
if(session.isOpened()){
列表< 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);
会话currentSession = Session.getActiveSession();
if(currentSession == null || currentSession.getState()。isClosed()){
会话session = Session.openActiveSession(this,true,fbStatusCallback); //问题:不需要许可,但是电话会执行在OPEN
currentSession = session;
}
if(currentSession!= null&!currentSession.isOpened()){
OpenRequest openRequest = new OpenRequest(this).setCallback(fbStatusCallback); //这里很好执行电话,因为我们已经获得了许可证
如果(openRequest!= null){
openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
openRequest.setPermissions(Arrays.asList(friends_hometown));
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
currentSession.openForRead(openRequest);
}
}
}
我在做什么在会话打开后立即请求许可,但此时该代码已经开始执行Graph API请求,因此权限请求迟到...
您不能在初始化会话的同时请求许可?
我建议您阅读我们的登录教程具体在步骤3中。使用我们提供的登录按钮是最方便的方法(见 authButton.setReadPermissions()
)
编辑: / p>
设置权限而不使用登录按钮是棘手的,因为您必须手动进行所有会话管理。挖掘登录按钮的源代码,这个可能是您需要的。看起来您将需要创建自己的 Session.OpenRequest
并设置其属性,如权限,受众和登录行为,然后获取当前会话并调用 openForRead()
在您的 Session.OpenRequest
。
With Facebook's new Android SDK 3.0 (that was released a few days ago), the process of authentication has changed.
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:
Version 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() {
...
});
}
}
});
}
Version 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);
}
}
}
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?
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()
)
EDIT:
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请求权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!