简体   繁体   English

Android-如何使用Facebook SDK 4.0.1发表评论

[英]Android - How to post a comment using Facebook SDK 4.0.1

Is there any way to post a comment using FacebookSDK 4.0.1. 有没有办法使用FacebookSDK 4.0.1发表评论。 Because Request was removed in new SDK. 因为在新的SDK中删除了请求。 Assume that I had login and got "publish_actions" permission. 假设我已经登录并获得了“ publish_actions”权限。

private void facebookLogin() {
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_friends"));
}

private void facebookLogout (){
    LoginManager.getInstance().logOut();
}

private void facebookPost() {
    //check login
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    if (accessToken == null) {
        Log.d(TAG, ">>>" + "Signed Out");
        status = Status.POST;
        facebookLogin();
        return;
    } else {
        Log.d(TAG, ">>>" + "Signed In");
        status = Status.NONE;
    }
    if (accessToken.getPermissions().contains("publish_actions")) {
        Log.d(TAG, ">>>" + "contain publish_actions");

        //I wanna post a comment in here
    } else {
        Log.d(TAG, ">>>" + "NOT contain publish_actions");

        LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions"));
    }
}

您是否尝试过使用com.facebook.share.widget.ShareApi类进行共享

ShareApi.share(content,null);

Assuming that you want to post something on a users wall, here are the steps you need to take in order to use Facebook Post, using Open Graph Stories 假设您要在用户墙上发布内容,以下是使用Open Graph Stories使用Facebook Post所需执行的步骤。

1) Head on to the Developer Console 1)前往开发人员控制台

2) Register your application, create the new login flow - be warned the older login flow does not work with SDK 4.0 2)注册您的应用程序,创建new登录流程-警告旧的登录流程不适用于SDK 4.0

3) Now once login is working for you, Head on to the Dashboard for this app, select Open Graph from the pane on left. 3)现在,一旦登录成功,请转到该应用程序的仪表板,然后从左侧窗格中选择“打开图形”。

4) Define your Actions, Stories and Objects here. 4)在此处定义您的动作,故事和对象。

5) Although Facebook has given an option to get code, next to the Stories, Action Types and Object Types - be warned this code is old and will not work. 5)尽管Facebook提供了获取代码的选项,但在故事,动作类型和对象类型旁边-请注意,此代码过旧并且无法使用。

Here is what works for me: 这对我有用:

My app name is (as defined on the Dev Console) 我的应用名称为(在开发控制台上定义)

friendssampleapp 老友记

My Action Type: Celebrating My Object Type: Milestone 我的动作类型:庆祝我的对象类型:里程碑

Here is the snippet of code I use to post via a button click: 这是我通过单击按钮发布的代码片段:

            ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
            .putString("og:type", "friendsampleapp:milestone")
            .putString("og:title", "A Game of Thrones")
            .putString("og:description", "In the frozen wastes to the north of Winterfell, sinister and supernatural forces are mustering.")
        //  .putPhoto("og:image", photo)
            .build();


            // Create an action
            ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
            .setActionType("friendsampleapp:celebrating")
            .putObject("milestone", object)
            .build();

            // Create the content
            ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
            .setPreviewPropertyName("milestone")
            .setAction(action)
            .build();

            ShareDialog.show(getActivity(), content); 

use below code: 使用以下代码:

Bundle params = new Bundle();
            params.putString("message", "This is a test message");

        new GraphRequest(
                accessToken,
                "/me/feed",
                params,
                HttpMethod.POST,
                new GraphRequest.Callback() {
                    public void onCompleted(GraphResponse response) {
                    }
                }
        ).executeAndWait();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM