如何使用Facebook Graph API访问令牌

时间:2012-08-14 12:13:05

标签: javascript html facebook facebook-graph-api

我正在尝试发布到用户的“活动Feed”,但我不明白如何。

这是我的演示应用程序: http://bazaar-market.co.uk/facebook_test/car.html

您可以查看来源以查看所有代码,因为它都是HTML。

<script type="text/javascript">
  function postView()
  {
      FB.api(
        '/me/fandango-auctions:view',
        'post',
        { item : 'http://bazaar-market.co.uk/facebook_test/car.html' },
        function(response) {
           if (!response || response.error) {
              alert("Error");
           } else {
              alert('View was successful! Action ID: ' + response.id);
           }
           console.log(response);
        });

  }
</script>

我收到错误:

  

必须使用app access_token

调用此方法

如果我在应用程序的设置中禁用了access_token,那么它会显示如下内容:

  

您必须使用access_token访问用户帐户详细信息

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

试试这个。

https://developers.facebook.com/docs/authentication/client-side/

<html> 
   <head> 
     <title>Client-side OAuth Example</title> 
   </head> 
   <body> 
   <script> 
     function displayUser(user) {
       var userName = document.getElementById('userName');
       var greetingText = document.createTextNode('Greetings, '
         + user.name + '.');
   userName.appendChild(greetingText);
     }

     var appID = YOUR_APP_ID;
     if (window.location.hash.length == 0) {
       var path = 'https://www.facebook.com/dialog/oauth?';
   var queryParams = ['client_id=' + appID,
     'redirect_uri=' + window.location,
     'response_type=token'];
   var query = queryParams.join('&');
   var url = path + query;
   window.open(url);
     } else {
       var accessToken = window.location.hash.substring(1);
       var path = "https://graph.facebook.com/me?";
   var queryParams = [accessToken, 'callback=displayUser'];
   var query = queryParams.join('&');
   var url = path + query;

   // use jsonp to call the graph
       var script = document.createElement('script');
       script.src = url;
       document.body.appendChild(script);        
     }
   </script> 
   <p id="userName"></p> 
   </body> 
  </html>

答案 1 :(得分:0)

错误在于函数:postView()。 当我登录时,我有access_token: AAAFIDJXVUKcBABpW5pcU97CC7sTwdDmJykOKhZAykZC6D8gaTumTsDZBRzCfJ3eqc58R5whIFuGTBnPeeRGmn6N0ccBDLRGNKXJmy5ZCexTvjBqExTmI

但是使用已经过期的另一个access_token函数postView():

FB.api(
        '/me/fandango-auctions:view',
        'post',
        {
        access_token : 'AAAFIDJXVUKcBALARwKxJHdwwf7chdqleGsrQtYsIk3xiB4vR111s3Gc4cos3Dgd6CFynoUURci0i3t7lzh5mZA41UqSJzbccjy6JjWLSTk8UfgvEO',
        item : 'http://bazaar-market.co.uk/facebook_test/car.html' 
        },
        function(response) {
           if (!response || response.error) {
              alert("Error");
           } else {
              alert('View was successful! Action ID: ' + response.id);
           }
           console.log(response);
        });