简体   繁体   English

使用客户端的FB API发布到Facebook页面

[英]Post to facebook page using fb api from client

I am trying to post to a users page . 我正在尝试发布到用户页面。 This is how far i have come , I get userId and userToken from FB.getLoginStatus and then post to my feed using this code . 这是我所走的路,我从FB.getLoginStatus获取了userId和userToken,然后使用此代码将其发布到我的提要中。 Now I want to post to a page but i keep getting you dont have permission to post to a page. 现在我想发布到页面,但是我一直在让你没有发布到页面的权限。

  FB.api('/me/feed', 'post', {
                           message:'This is my message',
                           link:"http://trello.com",
                            name: 'This is my message',
                            description: 'This is my message'
                        },function(data) {
                           console.log(data);
                            alert(data);
                       });
                        },
                     { scope: "publish_actions" });  

You are mixing some things up. 您正在混淆一些东西。 Permissions need to be asked for with FB.login , not when using the API: 需要使用FB.login来请求权限,而不是使用API​​时:

FB.login(function(response) {
    if (response.authResponse) {
        //user is logged in
    }
}, {scope: 'publish_actions', return_scopes: true});

After authorization, you can use the API: 授权后,您可以使用API​​:

FB.api('/me/feed', 'post', {
        message: 'This is my message',
        link: 'http://trello.com',
        name: 'This is my message',
        description: 'This is my message'
    }, function(data) {
        console.log(data);
    });
});

More information: http://www.devils-heaven.com/facebook-javascript-sdk-login/ 详细信息: http : //www.devils-heaven.com/facebook-javascript-sdk-login/

That´s for the user profile. 这是针对用户个人资料的。 Posting to a Page (as Page) needs the manage_pages and publish_pages permissions: 发布到页面(作为页面)需要manage_pagespublish_pages权限:

FB.login(function(response) {
    if (response.authResponse) {
        //user is logged in
    }
}, {scope: 'manage_pages,publish_pages', return_scopes: true});

You have to get a Page Token for your Page with this API call after authorizing: /{page-id}?fields=access_token : 授权后,必须通过以下API调用为您的页面获取一个页面令牌: /{page-id}?fields=access_token

FB.api('/' + pageId + '', {fields: 'access_token'}, function(response) {
    console.log(response);
});

After that, you can use the Token in the response to post as Page. 之后,您可以在响应中使用令牌将其发布为Page。

FB.api('/' + pageId + '/feed', 'post', {message: 'some message', access_token: pageToken}, function(response) {
    console.log(response);
});

Side Note: Prefilling the message and autoposting (right after login) is not allowed, make sure you read the platform policy before creating any App: https://developers.facebook.com/policy/ 旁注:不允许预填充消息和自动发布(登录后立即登录),请确保在创建任何应用程序之前阅读平台政策: https : //developers.facebook.com/policy/

You are using the wrong endpoint , you have to use '/pageid/feed'. 您使用的端点错误,必须使用'/ pageid / feed'。 After getting the userid and token you need to get all the pages he has permission for by hitting the '/me/accounts' . 获取用户标识和令牌后,您需要通过点击'/ me / accounts'获得他有权访问的所有页面。 Now you get the pageid of page you want to post from here and then hit '/pageid/feed' to post . 现在,您可以从此处获取要发布的页面的pageid,然后点击'/ pageid / feed'进行发布。 Here is some code: 这是一些代码:

FB.login(function(responses1) {
                        FB.api('/me/accounts', 'get',function(data) {
                        //you will get a list here.......//for now i am hardcoding it to the first page
                            var page_access_token = data.data[0].access_token;
                            var pageId = data.data[0].id;
                            //now post to the page
                            var target = '/'+pageId+'/feed'

                            FB.api(target,
                                'post',
                                { message: "message",
                                    link: "link",
                                    picture: "",
                                    caption: "",
                                    description: "",
                                    name: "",
                                    access_token: page_access_token
                                }
                                ,function(response) {
                                    if (!response || response.error) {
                                        console.log("in error");
                                       console.log(response.error);
                                    } else {
                                        console.log(response);
                                    }
                                });
                                    });
                    }, { scope: "manage_pages,publish_pages" });

Also you need to change scope to : 另外,您需要将范围更改为:

manage_pages,publish_pages

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

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