在过去的几天里,我一直在用 facebook-sdk-for-android-wall 敲打我的头,需要帮助。我找不到问题所在,我应该做什么,或者如何检查我缺少的某些状态。
我的 Android 应用程序正在录制曲目,对于曲目,用户必须选择将曲目摘要发布到他/她的 Facebook 时间线。我已经让它像一个快乐的流程一样工作,但是在测试不同的场景(例如没有互联网连接等)时,我现在陷入了一种状态,我只能得到“会话:尝试重新授权具有待处理请求的会话" - 尝试重新授权权限时出现异常(即使互联网连接已恢复)。我正在使用适用于 android 和 SSO 的 facebook SDK。
package com.test;
import java.io.Fil...
...
public class FaceBookUtil {
public interface FacebookPostCallback {
void onComplete(String postId);
void onError(Exception e);
void onError();
}
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions", "publish_stream");
private static final int REAUTH_ACTIVITY_CODE = 100;
public static void postToFaceBook(final Track track, final Activity activity, final FacebookPostCallback postCallback) {
try {
Session session = initFacebookSession(activity.getApplicationContext());
Session.setActiveSession(session);
StatusCallback statusCallback = new StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
try {
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
Session.ReauthorizeRequest reauthRequest = new Session
.ReauthorizeRequest(activity, PERMISSIONS)
.setRequestCode(REAUTH_ACTIVITY_CODE);
session.reauthorizeForPublish(reauthRequest); //Here's where it breaks and the exceptions is thrown.
return;
}
Bundle postParams = new Bundle();
postParams.putString("name", "Facebook post test");
postParams.putString("caption", "Just a test");
postParams.putString("description", "A description");
postParams.putString("link", "http://www.google.com/");
Request.Callback reqCallback = new Request.Callback() {
public void onCompleted(Response response) {
String postId = null;
try {
FacebookException error = response.getError();
if (error != null)
throw error;
JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
postId = graphResponse.getString("id");
if (postId != null)
postCallback.onComplete(postId);
} catch (Exception e) {
postCallback.onError();
}
if (postId == null)
postCallback.onError();
}
};
Request postRequest = new Request(session, "me/feed", postParams, HttpMethod.POST, reqCallback);
RequestAsyncTask task = new RequestAsyncTask(postRequest);
task.execute();
} catch (Exception e) {
postCallback.onError(e);
}
}
};
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED))
session.openForRead(new Session.OpenRequest(activity).setCallback(statusCallback));
else if (!session.isOpened() && !session.isClosed()) {
OpenRequest req = new Session.OpenRequest(activity);
req.setCallback(statusCallback);
session.openForRead(req);
} else {
Session.openActiveSession(activity, true, statusCallback);
}
} catch (Exception e) {
postCallback.onError(e);
}
}
private static boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
private static Session initFacebookSession(Context context) {
Session session = Session.getActiveSession();
if (session != null)
return session;
/*
if (savedInstanceState != null)
session = Session.restoreSession(this, null, sessionStatusCallback, savedInstanceState);
*/
if (session == null)
session = new Session(context);
return session;
}
}
如果我调试到 android SDK,我可以看到异常是由 Session 类中的 reauthorize 方法引发的,因为 pendingRequest 不为空,但我不明白为什么,它在哪里设置或如何检查它,删除它或者我应该做什么?
private void reauthorize(ReauthorizeRequest reauthorizeRequest, AuthorizationType authType) {
validatePermissions(reauthorizeRequest, authType);
validateLoginBehavior(reauthorizeRequest);
if (reauthorizeRequest != null) {
synchronized (this.lock) {
if (pendingRequest != null) { //Here's where the pendingRequest is already set and causes the exception...
throw new UnsupportedOperationException(
"Session: an attempt was made to reauthorize a session that has a pending request.");
}
switch (this.state) {
case OPENED:
case OPENED_TOKEN_UPDATED:
pendingRequest = reauthorizeRequest;
break;
default:
throw new UnsupportedOperationException(
"Session: an attempt was made to reauthorize a session that is not currently open.");
}
}
authorize(reauthorizeRequest);
}
}
我已经在没有任何结果的情况下搜索了异常,非常感谢任何帮助。
附带说明一下,我可能可以重构整个 FacebookUtil.postToFaceBook 方法,但在测试时我将其保留在一种方法中以提高可读性。