4

Facebookで投稿を共有しようとしています。ログイン、公開許可は初めて正常に機能しています。Facebook Webからアプリを削除すると、ログインの再インストールを求められず、下の「あなたに代わって投稿」の公開許可画面が私のコードです。私が間違えたものを助けてください。私はたくさん試しましたが、適切に動作していません..あなたの親切なアドバイスに感謝し、アンドロイドで多くを改善することができます. 以下は私のコードです...

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;
}
 }
4

1 に答える 1