开发者

Android: How to post update on Facebook wall automatically?

I want my android application to automatically post a preset message when the user click on a button. The preset message will be set by the user, so I am gues开发者_JAVA百科sing that is not a violation of Facebook policies. How do I do this?


private static final String[] PERMISSIONS =
    new String[] {"publish_stream", "read_stream", "offline_access"};


Facebook authenticatedFacebook = new Facebook(APP_ID);


postButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        authenticatedFacebook.authorize(Tests.this, PERMISSIONS,
            new TestPostListener());
    }
});


public class TestPostListener implements DialogListener {

    public void onComplete(Bundle values) {
        try {
            Log.d("Tests", "Testing request for 'me'");
            String response = authenticatedFacebook.request("me");
            JSONObject obj = Util.parseJson(response);

            Log.d("Tests", "Testing graph API wall post");
            Bundle parameters = new Bundle();
            parameters.putString("message", "Amit Siddhpura");
            parameters.putString("description", "Hi Mr. Amit Siddhpura");
            response = authenticatedFacebook.request("me/feed", parameters, 
                "POST");
            Log.d("Tests", "got response: " + response);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void onCancel() {
    }

    public void onError(DialogError e) {
        e.printStackTrace();
    }

    public void onFacebookError(FacebookError e) {
        e.printStackTrace();
    }
}


You have to create Application on Facebook

And get authenticate from user, then you can get a access_token to post some message through Graph API

I think your application have to request extended permissions : publish_stream, offline_access

There is Facebook-Android-SDK source code on github, you can refer it.

http://developers.facebook.com/docs/guides/mobile


http://www.androidpeople.com/android-facebook-api-example-using-fbrocket/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜