我正在使用新的 facebook SDK 3.0 开发 android 应用程序。我知道 facebook 的政策是我不能同时请求两个权限,也不能在登录按钮上请求写权限(publish_stream、publish_acition..etc)。
所以,我的问题是,如何在用户单击日志按钮后自动请求“publish_stream”权限?
在我下面的源代码中,
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
if (pendingPublishReauthorization && state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
pendingPublishReauthorization = false;
publishFeed(userName);
}
m_pd.show();
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
String username=buildUserInfoDisplay(user);
userName=username;
saveAccount(username, "facebook");
Log.d(com.dress.folio.Constants.FACEBOOK_TAG, "login success");
publishFeed(userName);
m_pd.dismiss();
//Activity GetActivity=getActivity();
//if(GetActivity != null) GetActivity.finish();
}
}
});
} else if (state.isClosed()) {
Log.i(com.dress.folio.Constants.FACEBOOK_TAG, "Logged out..." + exception);
}
}
当用户单击登录按钮时,会调用 onSessionStateChange()。所以我尝试在 Request.executeMeRequestAsync(session, new Request.GraphUserCallback() 之后用 'publishFeed()' 请求 'publish_stream'。
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
String username=buildUserInfoDisplay(user);
userName=username;
saveAccount(username, "facebook");
Log.d(com.dress.folio.Constants.FACEBOOK_TAG, "login success");
publishFeed(userName);
m_pd.dismiss();
//Activity GetActivity=getActivity();
//if(GetActivity != null) GetActivity.finish();
}
}
});
publishFeed() 就像:
private void publishFeed(String username) {
Session session = Session.getActiveSession();
SessionState state= session.getState();
if (session != null){
Log.d(com.dress.folio.Constants.FACEBOOK_TAG, "in publishstory(): "+ state);
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions) ) {
pendingPublishReauthorization = true;
if(state == SessionState.OPENED_TOKEN_UPDATED) {
Log.d(com.dress.folio.Constants.FACEBOOK_TAG, "in publishstory() if : OPENED_TOKEN_UPDATED" );
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
}
return;
}
Bundle postParams = new Bundle();
postParams.putString("name", username + " is logged in");
// postParams.putString("caption", "Build great social apps and get more installs.");
postParams.putString("description", "get the dress!!");
postParams.putString("link", "https://www.facebook.com/pages/Dressfolio/183665935091337"); //dressfolio facebook page address
postParams.putString("picture", "https://MY_APPLICATION.com/asset/facebook/app1/thumb2.png"); //thumbnail
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
Log.d(com.dress.folio.Constants.FACEBOOK_TAG, "callback()" );
FacebookRequestError error = response.getError();
if (error != null) {
Log.d(com.dress.folio.Constants.FACEBOOK_TAG,"news feed error!" + error.toString());
}
else {
Log.d(com.dress.folio.Constants.FACEBOOK_TAG, "News Feed Posting success()" );
}
}
};
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
但这总是不起作用,因为 Sessionstate 不处于“OPENED_TOKEN_UPDATED”状态。我怎样才能做到这一点?