使用Android Facebook sdk在wallpost中标记朋友

时间:2012-07-17 12:00:32

标签: tags facebook-android-sdk

我正在尝试使用android facebook sdk标记一个朋友在一个wallpost。然而,什么应该是标签,是空白,没有。这是我用过的代码:

            Bundle params = new Bundle();
            access_token = fb.getAccessToken();

            try {
                params.putString("format", "json");
                params.putString("access_token", access_token);
                String url = "https://graphs.facebook.com/me/friends";
                String response = Util.openUrl(url, "GET", params);
                JSONObject json = Util.parseJson(response);
                JSONArray array = json.optJSONArray("data");

                for(int i = 0; i < array.length(); i++) {
                    String tempName = array.getJSONObject(i).getString("name");
                    String tempID = array.getJSONObject(i).getString("id");

                    //Probably should have some if-tests here

                    if(tempName.contains(*nameOfFriend*)) {
                        Bundle bundle = new Bundle();
                        bundle.putString("message", "App tagging test");
                        //this is where the tagging is supposed to happen
                        bundle.putString("tags", *UserID*);
                        try {
                            fb.request("me/feed", bundle, "POST");
                            Toast.makeText(getApplicationContext(), "Tag-test", Toast.LENGTH_SHORT).show();
                        } catch (MalformedURLException e) {
                            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        } catch (IOException e) {
                            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "Couldn't find friend", Toast.LENGTH_SHORT).show();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

我只授予“publish_stream”权限,可能是我需要其他权限吗? 先谢谢你的帮助,伙计们!

2 个答案:

答案 0 :(得分:2)

以下是标记好友的工作代码 此外,您必须将您的评论提交给fb,以获取您在devlepoer facebook帐户中创建的项目的Taggable Friends api功能。获得批准后,以下代码将标记您的朋友。

     Bundle params = new Bundle();
    params.putString(Facebook.TOKEN, facebook.getAccessToken());
    params.putString("method", "photos.upload");
    params.putString("caption", ShareTripActivity.tripNotes); // text to post

    if(ShareTripActivity.arr_facebookID.size()>0)
    {
        String tagFriendListId="";
        for(int i=0;i<ShareTripActivity.arr_facebookID.size();i++)
        {

            tagFriendListId = tagFriendListId+"{'tag_uid':'"+ShareTripActivity.arr_facebookID.get(i)+"'} ,";

        }
        tagFriendListId=tagFriendListId.substring(0, tagFriendListId.length()-1);
         params.putString("tags","["+tagFriendListId+"]");

    }
   AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);

//示例上传监听器

公共类SampleUploadListener extends BaseKeyListener实现了RequestListener     {

    public void onComplete(final String response, final Object state)
    {
        try
        {
            // process the response here: (executed in background thread)
            Log.d("Facebook-Example", "Response: " + response.toString());
            JSONObject json = Util.parseJson(response);
            final String src = json.getString("src");

            // then post the processed result back to the UI thread
            // if we do not do this, an runtime exception will be generated
            // e.g. "CalledFromWrongThreadException: Only the original
            // thread that created a view hierarchy can touch its views."

        }
        catch (JSONException e)
        {
            Log.w("Facebook-Example", "JSON Error in response");
        }
        catch (FacebookError e)
        {
            Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
        }
    }

    public void onFacebookError(FacebookError e, Object state)
    {
        // TODO Auto-generated method stub

    }

    public Bitmap getInputType(Bitmap img)
    {
        // TODO Auto-generated method stub
        return img;
    }

    public int getInputType()
    {
        // TODO Auto-generated method stub
        return 0;
    }

    public void onIOException(IOException e, Object state)
    {
        // TODO Auto-generated method stub

    }

    public void onFileNotFoundException(FileNotFoundException e, Object state)
    {
        // TODO Auto-generated method stub

    }

    public void onMalformedURLException(MalformedURLException e, Object state)
    {
        // TODO Auto-generated method stub

    }
}

在这个arr_facebookID中是包含你要标记的朋友的facebook_user_id的arraylist。

答案 1 :(得分:0)

我在代码中看不到place的任何值,但在通过API制作的帖子中标记人员时,这是必需

https://developers.facebook.com/docs/reference/api/user/#posts

  

标签[...]注意:如果不指定地点,则无法指定此字段。