OAuthException:(#200)用户尚未授权应用程序执行此操作

时间:2014-05-14 04:19:03

标签: javascript facebook

我正试图从我的javascript发布到Fb页面我收到此错误

  

(#200)用户尚未授权应用程序执行此操作

     

" OAuthException"

我可以同时发布到我的FB墙,但不能发布到我所在的FB粉丝页面。请指导我出错的地方。我的代码:

        <input type="submit" class="btn" 
            onclick="postToFeed(); return false;" 
            value="Share with Friends"/>
        <input type="submit" class="btn" 
            onclick="postToPage(); return false;" 
            value="Share On Page"/>
        <p id='msg'></p>





        <script> 
          FB.init({appId: '{!appId}', status: true, cookie: true});


          function postToPage() {

          var page_id = '1426984900889247';
          FB.api('https://graph.facebook.com/' + page_id, {fields: 'access_token'}, function(resp) {
          console.log(resp);
          console.log(resp.access_token);
            if(resp.access_token) {
                FB.api('https://graph.facebook.com/' + page_id + '/feed',
                    'post',
                    { message: "{!posDetails.Name}",
                      description :'{!posDetails.CMSR__Job_Description__c}',
                      link : '{!siteUrl}',
                      picture: '{!posDetails.CMSR__Linked_In_Url__c}',
                      caption: '{!posDetails.CMSR__Summary__c}',
                      access_token: resp.access_token }
                     ,function(response) {
                     console.log(response);
                     });
                }
            });
            alert(resp);
         }

          function postToFeed() {
            var obj = {
              method: 'feed',
              link: '{!siteUrl}',
              picture: '{!posDetails.CMSR__Linked_In_Url__c}',
              name: '{!posDetails.Name}',
              caption: '{!posDetails.CMSR__Summary__c}',
              description: '{!posDetails.CMSR__Job_Description__c}'
            };

            function callback(response) {
                if (response['post_id']) {
                    var postId = response['post_id'].split('_')[1];
                    document.getElementById('msg').innerHTML = 
                        "Posted to your wall. "+
                        "<a href=\"https://www.facebook.com/permalink.php?"+
                        "id={!me.id}&v=wall&story_fbid="+postId+"\">View your post</a>";
                }
            }

            FB.ui(obj, callback);
          }
        </script>



        <script>

            $( document ).ready(function() {
                var nameID;
                FB.getLoginStatus(function(response) {
                    if (response.status === 'connected') {
                        FB.api('/me/accounts', function(response){
                            console.log(response);
                            console.log(response.data);

                            var data= response['data'];
                            var ids = new Array();
                            var name = new Array();


                            console.log('data');
                            console.log(data);

                            console.log('ID');
                            console.log(ids);

                            for(var i=0; i<data.length; i++){
                                ids[i] = data[i].id;
                                name[i] = data[i].name;
                                if(ids[i] != null){
                                     console.log(ids[i]);
                                     if(nameID == 'undefined'){
                                        nameID = ids[i]+':'+data[i].name+';';
                                     }
                                     else{
                                        nameID = nameID+' '+ids[i]+':'+data[i].name+';';
                                     }
                                }
                            }
                            setVar(nameID);
                            console.log('method called ');
                            console.log(ids);
                            console.log(name);
                            console.log(nameID);

                        });
                    }
                    else {
                            FB.login();
                            FB.api('/me/accounts', function(response){
                                console.log(response);
                            });
                    }
                });

                function setVar(param){
                    jQuery('[id$=myHiddenField]').val(param);
                    console.log('Param value '+param);
                    passStringToController();
                }

            });            
        </script>

1 个答案:

答案 0 :(得分:0)

错误表示您尚未获取权限。获取访问权限然后尝试。要写入用户Feed,您需要 publish_actions 权限。

我相信编辑您的Graph API调用代码就可以了。

FB.api('https://graph.facebook.com/' + page_id + '/feed',
    'post',
    { 
        message: "{!posDetails.Name}",
        description :'{!posDetails.CMSR__Job_Description__c}',
        link : '{!siteUrl}',
        picture: '{!posDetails.CMSR__Linked_In_Url__c}',
        caption: '{!posDetails.CMSR__Summary__c}',
        access_token: resp.access_token 
    },
    function(response) {
        console.log(response);
    });
   }, 
   {scope: 'publish_actions'}
);

最后一部分是获取权限的地方。 您还应该尝试使用Graph API调用,然后再将其放入代码中。 Here is a link for Graph call Explorer。尝试使用Access Tokens(Permissions)&amp; API版本。

如果您需要教程,可以在此链接http://lynda.com/Facebook-tutorials找到。虽然它不是免费的,但它是值得的。