Facebook wall post error from Android app?

假装没事ソ 提交于 2020-01-06 15:49:32

问题


I have successfully integrated FB with my app.I want to auto post some text,links,images to my facebook wall after giving app permission from facebook account.I have tried using below provided logic but am getting

{Response: responseCode: 403,graphObject: null,error: {errorCode: 200,errorType: 
OAuthException,errorMessege: (#200) The user hasn't authorized the application
to perform this action}, isFromCache:false

FacebookActivity.java

public class FacebookActivity extends FragmentActivity {

private MainFragment mainFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        mainFragment = new MainFragment();
        getSupportFragmentManager()
        .beginTransaction()
        .add(android.R.id.content, mainFragment)
        .commit();
    } else {
        // Or set the fragment from restored state info
        mainFragment = (MainFragment) getSupportFragmentManager()
        .findFragmentById(android.R.id.content);
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

MainFragment.java

public class MainFragment extends Fragment {
private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper; 

private Session.StatusCallback callback=new Session.StatusCallback(){
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }

};  

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Bundle params = new Bundle();
        params.putString("message", "Hey I am using this app");
        params.putString("name", "Dexter");
        params.putString("caption", "londatiga.net");
        params.putString("link", "http://www.londatiga.net");
        params.putString("description", "Dexter, seven years old dachshund who loves to catch cats, eat carrot and krupuk");
        params.putString("picture", "http://twitpic.com/show/thumb/6hqd44");

        String uri = "/me/feed?message=" + "Hey I am using this app" + "" + "&access_token=" + session.getAccessToken();

        uri = uri.replace(" ", "%20");

        new Request(

                session,
                uri,
                params,
                HttpMethod.POST,
                new Request.Callback() {
                       public void onCompleted(Response response) {
                                                         Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_LONG).show();
                                                }
                                            }
                                    ).executeAsync();
        Log.i(TAG, "Logged in...");
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, 
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_facebook, container, false);
    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);

    if(Session.getActiveSession().isOpened()){
        Toast.makeText(getActivity(), "logged in to FB", 1).show();
        authButton.setReadPermissions(Arrays.asList("email", "read_stream"));
        authButton.clearPermissions();
        authButton.setPublishPermissions(Arrays.asList("publish_actions"));
    }else{
        authButton.setFragment(this);
        authButton.setReadPermissions(Arrays.asList("public_profile"));
    }
    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);
}

@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);
}

}

回答1:


The error message means that you did not authorize the user with publish_actions. Test the Access Token in the Debugger to find out which permissions are authorized: https://developers.facebook.com/tools/debug/

Btw, you will not get read_stream approved in the review process: https://developers.facebook.com/docs/facebook-login/permissions/v2.3#reference-read_stream

...meaning, it will only work for Users with a role in the App (Admin, Developer, Tester).




回答2:


finally after lots of research i have modified my class correctly.Here i am posting my modified code that may help others.

public class MainFragment extends Fragment {
private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper; 

private Session.StatusCallback callback=new Session.StatusCallback(){
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }

};  

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
        publishStory(session);
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}


public void publishStory(Session currentSession) {
     Bundle params = new Bundle();
     params.putString("message", "Hey I am using this app");
        params.putString("name", "Dexter");
        params.putString("caption", "londatiga.net");
        params.putString("link", "http://www.londatiga.net");
        params.putString("description", "Dexter, seven years old dachshund who loves to catch cats, eat carrot and krupuk");
        params.putString("picture", "http://twitpic.com/show/thumb/6hqd44");                                                

     WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(getActivity(),currentSession, params))
       .setOnCompleteListener(new OnCompleteListener() {

       @Override
       public void onComplete(Bundle values,FacebookException error) {
        if (error == null) {
         // When the story is posted, echo the success
         // and the post Id.
         final String postId = values.getString("post_id");
         if (postId != null) {
             Intent i=new Intent(getActivity(),MainActivity.class);//FacebookActivity.class);
             startActivity(i);
             getActivity().finish();                               // do some stuff
         } else {
         // User clicked the Cancel button
         Toast.makeText(getActivity(),
           "Publish cancelled", Toast.LENGTH_SHORT).show();
         }
        } else if (error instanceof FacebookOperationCanceledException) {
         // User clicked the "x" button
         Toast.makeText(getActivity(),
          "Publish cancelled", Toast.LENGTH_SHORT).show();
        } else {
         // Generic, ex: network error
         Toast.makeText(getActivity(),
          "Error posting story", Toast.LENGTH_SHORT).show();
        }
       }

      }).setFrom("").build();
     feedDialog.show();

    }



@Override
public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, 
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_facebook, container, false);
    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);

    if(Session.getActiveSession().isOpened()){
        Toast.makeText(getActivity(), "logged in to FB", 1).show();
    }else{
        authButton.setFragment(this);
        authButton.setReadPermissions(Arrays.asList("public_profile"));
    }
    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);
}

@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);
}

}


来源:https://stackoverflow.com/questions/29766121/facebook-wall-post-error-from-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!