我想使用facebook sdk 3.5来获取用户数据,还允许我的应用程序在用户墙上发布
我已经成功制作了一个简单的应用程序,在移动设备上使用已经安装的fb应用程序来获取活动会话,但是会话总是以默认权限打开,我想以发布权限打开它以便能够在用户墙上发布
我总是得到(200#)用户没有对此应用程序进行身份验证
这是我的代码
public class FBSharingManager {
private static final String GRAPH_USER_LOADED = "graph_user_loaded";
private static final int REAUTH_ACTIVITY_CODE = 100;
private static final String[] PERMISSIONS = new String[] { "access_token",
"status_update", "user_birthday", "publish_stream",
"publish_actions", "email" };
private Activity mActivity;
private Session mSession;
private GraphUser mGraphUser;
private Bundle mSharingParameters;
public FBSharingManager(Activity activity) {
this.mActivity = activity;
loginAndGetSession();
}
public void synchPersonalProfileWithFaceBook() {
ConnectivityManager connectivityManager = (ConnectivityManager) mActivity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo.isConnected()) {
if (mGraphUser == null) {
getPersonalData();
} else {
broadcastGraphUser();
}
} else {
Log.i("Myservices2", "network unavailable");
String message = mActivity.getResources().getString(
R.string.network_unavailable);
}
}
private void loginAndGetSession() {
Session.openActiveSession (mActivity, true,
new Session.StatusCallback() {
@Override
public void call(final Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
mSession = session;
Session.setActiveSession(mSession);
}
}
});
}
private void requestPublishPermissions() {
if (mSession != null && mSession.isOpened()) {
Session.NewPermissionsRequest reauthRequest = new Session.NewPermissionsRequest(mActivity, PERMISSIONS).setRequestCode(REAUTH_ACTIVITY_CODE);
reauthRequest.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
mSession.requestNewPublishPermissions(reauthRequest);
}
}
private void getPersonalData() {
if (mSession != null && mSession.isOpened()) {
Request.newMeRequest(mSession, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
mGraphUser = user;
broadcastGraphUser();
}
}
}).executeAsync();
}
}
private void broadcastGraphUser() {
// mActivity.showDialog(((ProfileActivity)mActivity).LOADING_DIALOIG);
Log.i("fb user name", mGraphUser.getFirstName());
Toast.makeText(mActivity.getApplicationContext(),mGraphUser.getFirstName(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(GRAPH_USER_LOADED);
LocalBroadcastManager.getInstance(mActivity).sendBroadcast(intent);
// mActivity.removeDialog(((ProfileActivity)mActivity).LOADING_DIALOIG);
}
public void shareOnFB(Bundle bundle) {
ConnectivityManager connectivityManager = (ConnectivityManager) mActivity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo.isConnected()) {
mSharingParameters = bundle;
if (mSession != null && mSession.isOpened()) {
requestPublishPermissions();
postToWallWraper();
}
} else {
String message = mActivity.getResources().getString(
R.string.network_unavailable);
}
}
private void postToWallWraper() {
if (mSession != null && mSession.isOpened()){
Request.Callback callback = new Request.Callback() {
public void onCompleted(Response response) {
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(mActivity.getApplicationContext(),
error.getErrorMessage(), Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(mActivity.getApplicationContext(),
"Done", Toast.LENGTH_LONG).show();
}
}
};
Request request = new Request(mSession, "me/feed",
mSharingParameters, HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
public GraphUser getUser() {
return mGraphUser;
}
public void setUser(GraphUser user) {
this.mGraphUser = user;
}
public Session getSession() {
return mSession;
}
public void setSession(Session mSession) {
this.mSession = mSession;
}
}
这是活动代码
public class MainActivity extends FragmentActivity {
private FBSharingManager mFBSharingManager;
private TextView welcome;
private Button getuserdata, publishtowall;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFBSharingManager = new FBSharingManager(this);
welcome = (TextView) findViewById(R.id.welcome);
getuserdata = (Button) findViewById(R.id.getuserdata);
publishtowall = (Button) findViewById(R.id.publishtowall);
getuserdata.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mFBSharingManager.synchPersonalProfileWithFaceBook();
}
});
publishtowall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bundle postParams = new Bundle();
postParams.putString("name", "Facebook SDK for Android");
postParams.putString("caption", "Build great social apps and get more installs.");
postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
postParams.putString("link", "https://developers.facebook.com/android");
postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
mFBSharingManager.shareOnFB(postParams);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session session = Session.getActiveSession();
if (session != null) {
session.onActivityResult(this, requestCode, resultCode, data);
mFBSharingManager.setSession(session);
}
}
}
当我请求向我的会话添加权限时,它返回到我的活动onActivityResult但它不会更改权限
请帮助
答案 0 :(得分:1)
请参阅HelloFacebook示例应用程序,了解如何正确请求发布权限并发布。
您的代码的问题在于您正在请求发布权限(这是一个异步请求),并且在发出请求后立即发出请求,但是在发布请求时,您还没有收到来自发布权限请求,这就是帖子失败的原因。
另外,另外,为什么在发布发布权限请求时禁止SSO?