【问题标题】:Meteor retrieve Twitter credentialsMeteor 检索 Twitter 凭据
【发布时间】:2015-04-23 00:48:13
【问题描述】:

我目前有一个带有自定义登录(电子邮件/密码)的 Meteor 应用程序。我计划要求用户链接他们的 Twitter 帐户(如果可用,每个用户有多个 Twitter 帐户),以便我可以代表他们提出 API 请求(获取推文、发布推文、搜索推文等)

我相信要做到这一点,我需要用户的 oauth_token 和 oauth_secret。

我正在使用 Tim Haines 的 'twitter' 包来尝试实现这一点。我还使用以下资源: Twitter.requestCredential function response https://github.com/timhaines/test-accounts/blob/master/test-accounts.js

我可以打开登录对话框,输入 Twitter 用户名/密码,然后我会得到一个令牌字符串,每次登录尝试都不同。所以我知道Twitter.requestCredential() 正在工作。但是,当我将令牌传递给代码Twitter.retrieveCredential(token) 中的行时,它什么也不返回(未定义)。

这是我的客户端代码(直接来自上面的链接):

            Twitter.requestCredential(function(tokenOrError) {
            if(tokenOrError && tokenOrError instanceof Error) {
                console.log('Error:' + tokenOrError);
            } else {
                Meteor.call('retrieveTwitterCredential', tokenOrError, function(error, result) {
                    if(error)
                        console.log(error.reason);
                    else {
                        console.log('DEMO ONLY - DON\'T send unfiltered credentials to the client on a production app');
                        console.log('Result:', result);
                    }
                });
            }
        });

这是我的服务器端代码(直接来自上面的链接):

        console.log(token);
    credential = Twitter.retrieveCredential(token);
    console.log(credential);
        if(credential instanceof Error)
            throw new Meteor.Error(500, credential.message);
        else
            return credential;

“凭据”似乎未定义。在过去的两周里,我一直在努力解决这个问题,所以如果有人能提供帮助,我们将不胜感激。

提前致谢!

【问题讨论】:

    标签: meteor


    【解决方案1】:

    我找到了解决方案,所以我想发布它以防万一其他人遇到同样的问题。

    当你在异步函数内部调用 Meteor 方法时,你需要使用Meteor.bindEnvironment() 来包装回调并在 Meteor 方法中使用函数的结果。

    这是对我有用的解决方案:

    Twitter.get('statuses', id, Meteor.bindEnvironment(function(error, response) {
                        // handle the error
                    } else {
                        //do stuff with the response
                    } 
    }, function() { console.log('Failed to bind environment'); }));
    

    附:这不仅适用于 Twitter 身份验证,还适用于任何异步功能。我必须使用 Meteor.bindEnvironment() 来存储异步函数的结果,我需要通过调用 Meteor 方法将其写入数据库。它也适用于此。

    希望对有需要的人有所帮助!

    【讨论】:

      猜你喜欢
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 2013-07-07
      • 2013-04-24
      • 1970-01-01
      • 2013-07-18
      • 2015-03-06
      • 2014-12-29
      相关资源
      最近更新 更多