gpt4 book ai didi

android - facebook 代表您发帖在 android facebook sdk 3.0.2 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:42 24 4
gpt4 key购买 nike

我正在尝试在 Facebook 上分享帖子。登录,发布权限第一次正常工作。如果我从 Facebook 网站上删除该应用程序,它不会要求我重新安装登录名和“代表您发帖”发布权限屏幕下面是我的代码请帮助我弄错了。我尝试了很多,但无法正常工作。谢谢你的建议,所以我可以在 android 中改进很多。下面是我的代码...

public class FacebokView extends Activity {
static final String applicationId = String.valueOf(R.string.app_id);
Session session;

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
private boolean pendingPublishReauthorization = false;
private UiLifecycleHelper uiHelper;
//private StatusCallback statusCallback;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.faceok_view);

// create the session
this.session = createSession();
Log.i("session1--->", "session"+ session.isOpened());

// start Facebook Login
//Session.openActiveSession(this, true, new Session.StatusCallback() {
StatusCallback statusCallback = new StatusCallback() {

// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {//READ
onSessionStateChange(session, state, exception);
Log.i("OPENED_TOKEN_UPDATED", state.equals(SessionState.OPENED_TOKEN_UPDATED) + "<<test");
if (session.isOpened() && session.getPermissions().contains("publish_actions")) {
Log.i("fb session1: publish_actions else if", session.getPermissions().contains("publish_actions") +"OPENED_TOKEN_UPDATED ----"+state.equals(SessionState.OPENED_TOKEN_UPDATED));
publishStory();
Log.i("session2--->", "session"+ session.isOpened());

} else if (session.isOpened()) {
Log.i("session3--->", "session"+ session.isOpened());
Log.i("fb session2: publish_actions else if", session.getPermissions().contains("publish_actions") +"OPENED_TOKEN_UPDATED ----"+state.equals(SessionState.OPENED_TOKEN_UPDATED));
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(FacebookView.this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
}
} else if(session != null && session.getState() == SessionState.CLOSED_LOGIN_FAILED){
Log.i("fb session:", "not open");
Log.i("session4--->", "session"+ session.isOpened());
finish();
}
}
};

if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)){
Log.i("session5--->", "session"+ session.isOpened());
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
Log.i("CREATED_TOKEN_LOADED", "session"+ session.isOpened());
}else if (!session.isOpened() && !session.isClosed()) {
Log.i("session6--->", "session"+ session.isOpened());
OpenRequest req = new Session.OpenRequest(this);
req.setCallback(statusCallback);
session.openForRead(req);
} else {
Log.i("session7--->", "session"+ session.isOpened());
Session.openActiveSession(this, true, statusCallback);

}

}

private void onSessionStateChange(Session session, SessionState state,Exception exception) {
if (state.isOpened()) {
if (pendingPublishReauthorization && state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
Log.i("onSessionStateChange--->OPENED_TOKEN_UPDATED -->", state.equals(SessionState.OPENED_TOKEN_UPDATED) + "pendingPublishReauthorization-->" + pendingPublishReauthorization);

pendingPublishReauthorization = false;
publishStory();
Log.i("session12--->", "session"+ session.isOpened());
}

} else if (state.isClosed()) {
Log.i("session8--->", "session"+ session.isOpened());
// shareButton.setVisibility(View.INVISIBLE);
}
}


protected void publishStory() {
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;
}

// after login make request to the /me API
Bundle parameters = getIntent().getExtras();
String facebookResponse = parameters.getString("facebookResponse");
Log.i("fbResp from server 1 publish_actions:", session.getPermissions().contains("publish_actions") + "*---facebookResponse----*"+ facebookResponse);

try {
JSONObject jsonObj = new JSONObject(facebookResponse);
parameters.putString("message", jsonObj.getString("MsgForFb"));
parameters.putString("name", jsonObj.getString("NameForFb"));
parameters.putString("link", jsonObj.getString("LinkForFb"));
parameters.putString("Description", jsonObj.getString("DescForFb"));
parameters.putString("picture", jsonObj.getString("PicForFb"));
} catch (JSONException e1) {
e1.printStackTrace();
}
Request.Callback callback = new Request.Callback() {
public void onCompleted(Response response) {
if(response.getGraphObject() != null) {
Log.i("fb Response - ", response.toString());
JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
e.printStackTrace();
Log.e("FacebookView: callBack:", "JSON error : " + e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(FacebookView.this.getApplicationContext(), error.getErrorMessage(), Toast.LENGTH_LONG).show();
} else {
Log.i("FacebookView: Request.Callbak: postId = ", " "+postId);
}
}
}
};

Request request = new Request(session, "me/feed", parameters, HttpMethod.POST, callback);

RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
finish();
}
}
});
}

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}



private Session createSession() {
Session activeSession = Session.getActiveSession();
if (activeSession == null || activeSession.getState().isClosed()) {
activeSession = new Session.Builder(this).setApplicationId(applicationId).build();
Session.setActiveSession(activeSession);
}
return activeSession;
}

private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
}

最佳答案

如果用户从 Facebook 删除了该应用,您的应用唯一知道的方式是您发出图表请求,但它失败了。

以下是一些常见的错误处理策略 - https://developers.facebook.com/docs/reference/api/errors/

您还应该查看 SDK 附带的 Scrumptious 示例,SelectionFragment.java 中有一个 handleError() 方法可实现上述链接中概述的策略。

关于android - facebook 代表您发帖在 android facebook sdk 3.0.2 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17878500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com