我正在使用facebook 3.5 api为Android应用程序的用户实现一些简单的文本共享。我确实有它工作,但登录过程有点不稳定和丑陋。
我将共享隔离到一个活动(因为这是一个黑客,以掩盖facebook不允许我们直接与他们的应用程序共享文本(甚至用户编辑)的事实。因此,Facebook登录经常发生正确在发送文本(可接受)之前,还请求用户两次确认权限请求(一次用于读取,一次用于写入),有时在两者上单击“是”之后,登录会话将丢失并再次请求原始用户登录(这非常难看!)。
以下是我在下面使用的代码:
来自onCreate()的:
mSession = Session.getActiveSession();
if(mSession!=null){
if(!mSession.isOpened()){
mSession.openForRead(new Session.OpenRequest(FBShareActivity.this).setCallback(mStatusCallback));
Session.setActiveSession(mSession);
}
}else {
mSession = new Session(FBShareActivity.this);
mSession.openForRead(new Session.OpenRequest(FBShareActivity.this).setCallback(mStatusCallback));
Session.setActiveSession(mSession);
}
从我的墙贴代码:
if(mSession!=null) {
// Check for publish permissions
List<String> permissions = mSession.getPermissions();
if (!isSubsetOf(SHARE_TEXT_PERMISSIONS, permissions)) {
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, SHARE_TEXT_PERMISSIONS);
mSession.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Bundle postParams = new Bundle();
postParams.putString("name", book.getTitle());
if(msgTitle!=null&&msgTitle.length()>0)
postParams.putString("caption", msgTitle);
postParams.putString("message", quote);
if(mReceivedUri!=null&&mReceivedUri.toString().length()>0) {
//TODO: Actually we need to put the image data in here!!
byte[] data = null;
Log.e(TAG, "[shareBookQuote] image uri = "+mReceivedUri.toString());
Bitmap bi;
try {
bi = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mReceivedUri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
postParams.putByteArray("photo", data);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Failed to locate image file to share", e);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Failed to load image file to share", e);
} catch (Exception e) {
Log.e(TAG, "Failed to load image file to share", e);
}
}
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
if(response!=null) {
String postId = null;
GraphObject gObj = response.getGraphObject();
if(gObj!=null){
JSONObject graphResponse = gObj.getInnerJSONObject();
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(TAG,
"JSON error "+ e.getMessage());
}
}
FacebookRequestError error = response.getError();
if (error != null) {
String errorTitle = getString(R.string.fb_error_title);
String errorMessage = error.getErrorMessage();
int errorCode = error.getErrorCode();
if(errorCode==100) {
//Tried to send empty message
errorMessage = getString(R.string.fb_error_no_message);
showFragmentMessage(errorTitle, errorMessage);
} else {
//Other error, just report it to the user
showFragmentMessage(errorTitle, errorMessage);
}
//Make this an error dialog instead of a toast!!
} else {
returnToApp(); //My own fxn for returning to the app
}
}
}
};
if(mReceivedUri==null) {
Request request = new Request(mSession, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
} else {
Request request = new Request(mSession, "me/photos", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
此外:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
尽管此代码确实有效,但它确实存在上述多次登录和过多用户权限请求问题的问题。有什么我做错了,或者这是facebook API应该如何工作?
答案 0 :(得分:0)
事实证明,我在从facebook请求权限时使用了错误的权限名称:
private static final List<String> SHARE_TEXT_PERMISSIONS = Arrays.asList("publish_actions");
正确的权限是:
private static final List<String> SHARE_TEXT_PERMISSIONS = Arrays.asList("publish_stream");
即使publish_actions确实提取了正确的权限,当我检查我是否拥有正确的权限时,我总是拥有publish_stream。切换到请求publish_stream直接解决了这个问题。