[英]Android Facebook 3.5 sharing ugly login issues
我正在使用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的工作方式?
原来,我从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可以直接解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.