我收到错误
java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that has a pending request.
以下是我的代码,
public class MainActivity extends Activity {
private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper;
WebView webView;
Button nextButton;
LoginButton authButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
authButton = (LoginButton) findViewById(R.id.authButton);
authButton.setReadPermissions(Arrays.asList("email","user_birthday","basic_info"));
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
}
private void onSessionStateChange(final Session session,
SessionState state, Exception exception) {
if (state.isOpened()) {
// ///////
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
String access_token = session.getAccessToken();
String email = user.asMap().get("email")
.toString();
Log.i("user",
"user fname====" + user.getFirstName());
Log.i("user",
"user lname======="
+ user.getLastName());
Log.i("user",
"user username====="
+ user.getUsername());
Log.i("user", "user email333======"
+ (String) response.getGraphObject()
.getProperty("email"));
Log.i("user", "user email new222======" + email);
Log.i("user", "user email new111======"
+ user.getProperty("email").toString());
Log.i("user",
"user DOB======" + user.getBirthday());
Log.i("user",
"user getName======" + user.getName());
Log.i("user", "access_token======"
+ access_token);
}
}
});
Request.executeBatchAsync(request);
// /////////
Log.i(TAG, "Logged in...");
authButton.setVisibility(View.VISIBLE);
}
else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
System.out.println("statusCallBack Called");
onSessionStateChange(session, state, exception);
}
};
@Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
答案 0 :(得分:2)
在您的活动中使用此代码。
authButton.setPublishPermissions("publish_actions", "email",
"user_birthday", "basic_info");
答案 1 :(得分:2)
为了在用户实际想要发布某些内容,某些活动以及不使用“UiLifecycleHelper”之前提供发布权限。这个方法对我有用(基于许多试验和本特定教程中的代码: https://developers.facebook.com/docs/games/mobile/android-tutorial/ https://developers.facebook.com/docs/facebook-login/permissions/)
首先创建权限请求(如果用户没有):
Session session = Session.getActiveSession();
if (PostOnFB.isChecked() && session != null && session.isOpened()) {
List<String> permissions = session.getPermissions();
if (!permissions.contains("publish_actions")) {
// if they choose to publish, then we request for publish permissions
Session.NewPermissionsRequest newPermissionsRequest =
new Session.NewPermissionsRequest(GetUserLocationActivity.this, Arrays.asList("publish_actions"))
.setDefaultAudience(SessionDefaultAudience.FRIENDS)
.setCallback(newPermissionsCallback)
.setRequestCode(REAUTH_ACTIVITY_CODE);
session.requestNewPublishPermissions(newPermissionsRequest);
} else {
publishResult();
}
}
其中“publishResult()”是在用户获得权限后调用的方法。
接下来,为onActivityResult添加一个案例:
@Override
public void onActivityResult (
int requestCode, int resultCode, Intent data) {
// Decide what to do based on the original request code
switch (requestCode) {
case ..
..
..
..
..
case REAUTH_ACTIVITY_CODE :
Session.getActiveSession().onActivityResult(GetUserLocationActivity.this, requestCode, resultCode, data);
}
}
最后实现回调:
private Session.StatusCallback newPermissionsCallback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (exception != null || !session.isOpened() || !session.getPermissions().contains("publish_actions")) {
// this means the user did not grant us write permissions, so
// do whatever you think
} else {
publishResult();
}
}
};
就是这样。
答案 2 :(得分:0)
这是工作代码,只需尝试此Facebook login data
只需在此方法中创建此方法
private void makeMeRequest(final Session session) {
Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
public void onCompleted(GraphUser user, Response response) {
try{
Log.i("user", "user fname====" + user.getFirstName());
Log.i("user", "user lname=======" + user.getLastName());
Log.i("user", "user username=====" + user.getUsername());
createFacebookConnection(session);
} catch (Exception e) {
e.printStackTrace();
}
}
});
request.executeAsync();
}
facebook与facebook墙共享数据
public void createFacebookConnection(Session session) {
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
Session.StatusCallback statusCallback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
String message = "Facebook session status changed - "
+ session.getState() + " - Exception: " + exception;
Log.w("Facebook test", message);
if (session.isOpened() || session.getPermissions().contains("publish_actions")) {
publishToWall(session);
// share(session);
} else if (session.isOpened()) {
/* OpenRequest open = new OpenRequest(FacebookShareActivity.this).setCallback(this);
ArrayList<String> permission = new ArrayList<String>();
permission.add("publish_actions");
open.setPermissions(permission);
Log.w("Facebook test", "Open for publish");
session.openForPublish(open);*/
}
}
};
if (!session.isOpened() && !session.isClosed()&& session.getState() != SessionState.OPENING) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
} else {
Log.w("Facebook test", "Open active session");
Session.openActiveSession(this, true, statusCallback);
}
}