Meteor:在 accounts-facebook 之外实现 facebook 包

Meteor: Implement facebook package outside of accounts-facebook

我有一个带有多阶段注册过程的 Meteor 应用程序。这些帐户基于 accounts-password 包。在创建帐户之前的步骤中,用户需要提供一些个人资料信息。

我希望用户能够启动 Facebook OAuth 流程,该流程使用从 Facebook 提取的信息预填充个人资料字段。

这一切都需要在创建帐户之前发生。我想用支持 accounts-facebook.

facebook 包来实现这个

目前我通过调用 Facebook.requestCredential 获得了 OAuth 流程,但我不确定如何从返回的凭据令牌中获取 OAuth 访问令牌。我怀疑我需要将其传递给服务器并进行 API 调用以取回访问令牌。

任何关于这应该如何工作的指示将不胜感激。

Facebook.requestCredential(function (credentialTokenOrError) {
  if (credentialTokenOrError && credentialTokenOrError instanceof Error) {
    // Error...
    console.log(credentialTokenOrError);
  } else {
    // Credential Token string
    console.log(credentialTokenOrError);
    // Now perhaps a Meteor.call to a server method that
    // 1. Retrieves an access token
    // 2. Hits the graph API to get profile information and returns it to the client
  }
});

谢谢, 克里斯

我在将 credentialToken 转换为 accessToken 时遇到了同样的麻烦,只是 Github。我编写了一个 gist,其中的代码应该非常相似地工作。本质上,有两个步骤:

  1. 在您的 Facebook.requestCredential 回调函数中,调用 OAuth._retrieveCredentialSecret(tokenOrError),其结果是 credentialSecret。然后使用 Meteor.call,传入 tokenOrErrorcredentialSecret,调用您将在下一步中设置的 Meteor.method

代码(在客户端):

Github.requestCredential({
  loginStyle: 'popup',
  requestPermissions: ['gist']
}, function(tokenOrError) {
  if (tokenOrError && tokenOrError instanceof Error) {
    // Throw a Meteor error
    console.log('error getting the token');
    return;
  }
  var credentialSecret = OAuth._retrieveCredentialSecret(tokenOrError);
  Meteor.call('getGithubAccessToken', tokenOrError, credentialSecret, function(err, accessToken) {});
});
  1. 在服务器上,设置一个 Meteor.method 获取您的 credentialTokencredentialSecret 并调用 Facebook.retrieveCredential。此函数 returns 来自 _pendingCredentials Mongo 集合的凭据对象,然后再将其从集合中删除。访问令牌是 credentials.serviceData.accessToken。凭据对象可能会保留在 Meteor.users 集合中的用户对象中(因为它在 accounts 包中)或发送回用户。

代码(在服务器上):

  Meteor.methods({
    getGithubAccessToken: function(credentialToken, credentialSecret) {
      var credentials = Github.retrieveCredential(credentialToken, credentialSecret);
      console.log('accessToken:', credentials.serviceData.accessToken);
      return credentials.serviceData.accessToken;
    }
  });

我不熟悉 Facebook Graph 的细节 API 所以完成这些步骤后,您就只能靠自己了。祝你好运!