使用Auth0和Aurelia检索Facebook用户好友列表

时间:2016-10-25 10:45:31

标签: facebook-graph-api aurelia auth0

我在Aurelia单页应用程序中使用Auth0进行用户身份验证。我可以使用以下代码在我的app.js

中成功登录和注销
login() {
  this.lock.show();   
}

在构造函数中:

this.lock.on('authenticated', (authResult) => {
     self.lock.getProfile(authResult.idToken, (error, profile) => {
        if (error) {
            // Handle error
            console.log(error);
            return;
        }

        localStorage.setItem('id_token', authResult.idToken);
        localStorage.setItem('profile', JSON.stringify(profile));
        self.isAuthenticated = true;
        self.lock.hide();
    });
}

我现在想要调用Facebook Graph API并检索登录用户的朋友列表。我已经设置了为此功能授予应用访问权限的权限。我正在尝试使用这个获取模块的朋友列表:

this.http.fetch('https://graph.facebook.com/v2.5/{user-id}/friends', {
    method: 'GET',
    headers: {
        'Authorization': 'Bearer ' + authResult.idToken
    }
})
    .then(response => response.json())
    .then(friends => this.friends = friends);

但是我收到错误"要求此资源需要访问令牌"。我不知道我是否正确地做到了这一点。我已经阅读了文档,但我被困住了。我将非常感谢您提供的任何帮助。谢谢你们!

1 个答案:

答案 0 :(得分:1)

您需要使用Facebook发出的访问令牌调用Facebook API,而不是在身份验证过程中发出的Auth0 id_token

  通过对包含Auth0 API令牌的/api/v2/user/{user-id}端点进行HTTP GET调用,用户通过IdP进行身份验证后,可以获取身份提供商(在本例中为Facebook)访问令牌使用read:user_idp_tokens范围生成。

有关此内容的详细信息,请参阅现有文档中有关Identity Provider Access Tokens的主题。该页面还链接到有关如何Call an Identity Provider API的分步教程。