【问题标题】:Meteor: Implement facebook package outside of accounts-facebookMeteor:在account-facebook之外实现facebook包
【发布时间】:2015-09-27 20:43:41
【问题描述】:

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

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

这一切都需要在创建帐户之前进行。我想用支持accounts-facebookfacebook 包来实现这一点。

目前,我通过调用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
  }
});

谢谢, 克里斯

【问题讨论】:

    标签: facebook meteor oauth meteor-accounts


    【解决方案1】:

    我在将 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。此函数从 _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 的细节,所以在完成这些步骤之后,您就只能靠自己了。祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多