阅读FB发布tutorial
后我试图实现简单的发布,但在我的应用程序中,用户不必通过FB登录。 如果需要,我想打开登录名,如果是这样的话。
我得到了下一个例外:
04-28 12:15:57.350: E/AndroidRuntime(32241): FATAL EXCEPTION: main
04-28 12:15:57.350: E/AndroidRuntime(32241): java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that has a pending request.
04-28 12:15:57.350: E/AndroidRuntime(32241): at com.facebook.Session.requestNewPermissions(Session.java:968)
04-28 12:15:57.350: E/AndroidRuntime(32241): at com.facebook.Session.requestNewPublishPermissions(Session.java:501)
我的代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
boolean isFacebookResponse =
Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
if (isFacebookResponse) {
shareViaFacebook();
}
}
private void CheckFacebookLogin(){
if(Session.getActiveSession() == null){
logInToFacebook();
}else{
shareViaFacebook();
}
}
private void shareViaFacebook() {
Session session = Session.getActiveSession();
if (session != null){
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
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");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(GA_CATEGORY,
"JSON error "+ e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(getActivity()
.getApplicationContext(),
error.getErrorMessage(),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity()
.getApplicationContext(),
postId,
Toast.LENGTH_LONG).show();
}
}
};
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
private void logInToFacebook() {
String app_id = getString(R.string.app_id);
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
Session session = new Session.Builder(getActivity())
.setApplicationId(app_id)
.build();
session.addCallback(this);
Session.setActiveSession(session);
// Login
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this)
.setCallback(this));
} else {
Session.openActiveSession(getActivity(), true, this);
}
}
登录代码有效。 我做错了什么?
答案 0 :(得分:1)
我认为您正在尝试同时登录和共享操作。您应该等到登录操作完成所需的权限。然后,您可以通过您的申请分享您的内容。希望这会有所帮助。