2012-05-06 91 views
2

我有一个应该从Facebook对象获取评论的页面,并使用JavaScript将它们发布到页面上,但是当用户登录时,我无法为我的生活弄清楚如何获取OAuth令牌。这是我的页面。如何从JavaScript API获取OAuth令牌?

<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
      appId: 'myrealappid', 
      status: true, 
      cookie: true, 
      xfbml: true, 
      oauth: true, 
     }); 
    }; 
    (function (d) { 
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; } 
     js = d.createElement('script'); js.id = id; js.async = true; 
     js.src = "//connect.facebook.net/en_US/all.js"; 
     d.getElementsByTagName('head')[0].appendChild(js); 
    }(document)); 
    function getComments(objectid) { 
//I need to append the OAuth token to this graph request 
     var commentUri = "https://graph.facebook.com/" + objectid + "/comments"; 
     return $.getJSON(commentUri, function (json) { 
      var html = "<ul>"; 
      $.each(json.data, function (i, fb) { 
       html += "<li>" + fb.message + "</li>"; 
      }); 
      html += "</ul>" 
     }); 
     $('.comments').html(html); 
    }; 
    $(document).ready(function() { 
     getTripComments(data.WallPostId); 
    }); 
</script> 
<div id="pageLogo"> 
    <img src="/images/page_logo.png" alt="Digital Mementos" /> 
</div> 
<div id="container"> 
    <div class="fb-login-button">Login with Facebook to Comment</div> 
    <div id="comments"> 
    </div> 
</div> 

看看它说'我需要附加OAuth令牌到这个图请求'吗?是的,我需要这样做。我如何获得OAuth令牌?或者,我是否全力以赴?

回答

1

您错过了需要检查身份验证的部分。更多herestatus & sessions.

如果您检查以下你将不再需要访问令牌作为甘草说:

FB.login(function(response) { 
    if (response.authResponse) { 
    FB.api('/me', function(response) { 

    }); 
    } else { 

    } 
}); 

如果仍然需要一个访问令牌,你可以得到这样的:

FB.login(function(response) { 
    if (response.authResponse) { 
    var access_token = response.authResponse.accessToken; 

    } else { 

    } 
}); 
+0

谢谢!链接和代码很有用。我仍然在搞清楚“正确”的做事方式。 –

+0

很高兴帮助... –

0

与其向请求发送到完全限定的端点,请使用Facebook的JavaScript方法FB.api。当你使用这种方法时,你不需要担心令牌。

相关问题