何时通过Facebook的新Android SDK 3.0请求权限?
随着Facebook的新的Android SDK 3.0(前几天发布),authentication过程发生了变化。
那么你如何申请阅读许可,如“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) { // 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请求,因此权限请求会迟到。
您是否可以在初始化会话的同时请求许可?
我build议您在第3步中详细阅读我们的login教程。使用我们提供的loginbutton是最方便的方法(请参阅authButton.setReadPermissions()
)
编辑:
不使用loginbutton来设置权限是棘手的,因为你必须手工完成所有的会话pipe理。 挖掘loginbutton的源代码,这行代码可能是你需要的。 它看起来像你将需要创build自己的Session.OpenRequest
并设置它的属性,如权限,受众和login行为,然后获取当前会话,并在Session.OpenRequest
上调用openForRead()
。
我能够得到它的工作。 这是您的第2版样本的修改。 Jesse提供的链接也帮了大忙。
这是我在validation用户时运行的代码:
private void signInWithFacebook() { mSessionTracker = new SessionTracker(getBaseContext(), new StatusCallback() { @Override public void call(Session session, SessionState state, Exception exception) { } }, null, false); String applicationId = Utility.getMetadataApplicationId(getBaseContext()); mCurrentSession = mSessionTracker.getSession(); if (mCurrentSession == null || mCurrentSession.getState().isClosed()) { mSessionTracker.setSession(null); Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build(); Session.setActiveSession(session); mCurrentSession = session; } if (!mCurrentSession.isOpened()) { Session.OpenRequest openRequest = null; openRequest = new Session.OpenRequest(SignUpChoices.this); if (openRequest != null) { openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS); openRequest.setPermissions(Arrays.asList("user_birthday", "email", "user_location")); openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK); mCurrentSession.openForRead(openRequest); } }else { Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() { @Override public void onCompleted(GraphUser user, Response response) { Log.w("myConsultant", user.getId() + " " + user.getName() + " " + user.getInnerJSONObject()); } }); } }
对于testing,我从Facebookauthentication返回后通过下面的代码运行它:
public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); if (mCurrentSession.isOpened()) { Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() { // callback after Graph API response with user object @Override public void onCompleted(GraphUser user, Response response) { Log.w("myConsultant", user.getId() + " " + user.getName() + " " + user.getInnerJSONObject()); } }); } }
我通过实现我自己的Session.openActiveSession()
方法解决了同样的问题:
private static Session openActiveSession(Activity activity, boolean allowLoginUI, StatusCallback callback, List<String> permissions) { OpenRequest openRequest = new OpenRequest(activity).setPermissions(permissions).setCallback(callback); Session session = new Builder(activity).build(); if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) { Session.setActiveSession(session); session.openForRead(openRequest); return session; } return null; }
尽pipe第一个问题在几个月前被问过,并且有被接受的答案,但是还有另外一个更优雅的解决scheme,用于在authentication时向用户请求更多的权限。 另外,最近发布了Facebook SDK 3.5 ,这将是很好的刷新这个线程:)
所以,优雅的解决scheme来自这个开源的库: android-simple-facebook
build立
只需在Activity
类中添加下列行:
-
定义和select你需要的权限 ,比如
friends_hometown
:Permissions[] permissions = new Permissions[] { Permissions.FRIENDS_HOMETOWN, Permissions.FRIENDS_PHOTOS, Permissions.PUBLISH_ACTION };
这里有权限的伟大的事情是,你不需要单独的READ和PUBLISH权限。 你只是提到你需要什么,图书馆会照顾其余的。
-
通过放置
app_id
,namespace
和permissions
构build和定义configuration:SimpleFacebookConfiguration configuration = new SimpleFacebookConfiguration.Builder() .setAppId("625994234086470") .setNamespace("sromkuapp") .setPermissions(permissions) .build();
-
并且,创build
SimpleFacebook
实例并设置此configuration:SimpleFacebook simpleFacebook = SimpleFacebook.getInstance(Activity); simpleFacebook.setConfiguration(configuration);
现在,您可以运行方法,如: login , 发布饲料/故事 , 邀请 ,…
login
mSimpleFacebook.login(OnLoginListener);
登出
mSimpleFacebook.logout(OnLogoutListener);
有关更多示例和用法,请查看此页面: https : //github.com/sromku/android-simple-facebook#actions-examples