Specify privacy when POSTing to Facebook Graph API

后端 未结 5 1347
梦谈多话
梦谈多话 2020-12-09 19:05

I want to automatically post Notes on Facebook and have them be targeted to just a single member of a group. By target I mean only a specific Facebook user should be able t

5条回答
  •  囚心锁ツ
    2020-12-09 19:21

    Here's the answer.

    Just include "privacy" in the Bundle in JSONObject format, including value "SELF", "ALL_FRIENDS" OR "EVERYONE".

    This is using android SDK 2.0, and 3.0 is now avaliable, But the way to use graph api is the same, left comment if you reach any problem:).

    public String PostWall(String Message,int Level){
        /***********************************************************
            * level 0 ==>only me
            * level 1==>friend only
            * level 2==>public
            * level >2 ==>error
        ***********************************************************/
        Bundle params = new Bundle();
        params.putString("message", Message);
        JSONObject privacy = new JSONObject();
        try {
            switch (Level){
                case 0: 
                    privacy.put("value", "SELF");
                    break;
                case 1: 
                    privacy.put("value", "ALL_FRIENDS");
                    break;
                case 2: 
                    privacy.put("value", "EVERYONE");
                    break;
            }
        } catch (JSONException e1) {
        }
        params.putString("privacy", privacy.toString());
        //Step 2 Request
        String resp= "";
        try {
            resp = fb.request("me/feed", params, "POST");
        } catch (FileNotFoundException e) {
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        try{
            resp = new JSONObject(resp).getString("id");
            if(enableLog){
                Log.d(LOGTAG,"*****POSTWALL END*****");
                Log.d(LOGTAG,"RETURN "+resp);
            }
            return resp;
        }catch(JSONException e1){
        }
    }
    };
    

提交回复
热议问题