Meteor 检索 Twitter 凭据 [英] Meteor retrieve Twitter credentials

查看:72
本文介绍了Meteor 检索 Twitter 凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我正在使用 Tim Haines 的twitter"包来尝试实现这一点.我也在使用以下资源:Twitter.requestCredential 函数响应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('错误:' + tokenOrError);} 别的 {Meteor.call('retrieveTwitterCredential', tokenOrError, function(error, result) {如果(错误)控制台日志(错误.原因);别的 {console.log('DEMO ONLY - 不要在生产应用程序上向客户端发送未经过滤的凭据');console.log('结果:', 结果);}});}});

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

 console.log(token);credential = Twitter.retrieveCredential(token);控制台日志(凭据);如果(凭证实例错误)throw new Meteor.Error(500, credential.message);别的返回凭证;

凭据"似乎未定义.过去两周我一直在为此纠结,所以如果有人能提供帮助,我将不胜感激.

提前致谢!

解决方案

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

当你让一个 Meteor 方法调用 INSIDE 一个异步函数时,你需要使用 Meteor.bindEnvironment() 来包装回调并在 Meteor 方法中使用函数的结果.>

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

Twitter.get('statuses', id, Meteor.bindEnvironment(function(error, response) {//处理错误} 别的 {//对响应做一些事情}}, function() { console.log('绑定环境失败');}));

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

希望能帮到有需要的人!

I currently have a Meteor application with a custom login (email/password). I am planning on asking users to link their Twitter accounts (multiple Twitter accounts per user if available), so that I can make API requests on their behalf (get tweets, post tweets, search tweets, etc.)

I believe to do this, I need the user's oauth_token, and oauth_secret.

I am using Tim Haines' 'twitter' package to try to implement this. I am also using the following resources: Twitter.requestCredential function response https://github.com/timhaines/test-accounts/blob/master/test-accounts.js

I am able to pull up the login dialog, enter the Twitter username/password, and I get a token string, which is different with each login attempt. So I know that Twitter.requestCredential() is working. However, when I pass the token to the line in the code Twitter.retrieveCredential(token), it returns nothing (undefined).

Here is my client-side code (straight from the link above):

            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);
                    }
                });
            }
        });

And here is my server-side code (straight from the link above):

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

The 'credential' seems to be undefined. I've been pulling my hair over this for the last two weeks, so if anyone can help, that would be highly appreciated.

Thanks in advance!

解决方案

I found the solution to this, so I wanted to post it just in case anyone else is having the same issue.

When you make a Meteor method call INSIDE an async function, you need to use Meteor.bindEnvironment() to wrap up the callback and use the result of the function in the Meteor method.

Here is the solution that worked for me:

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'); }));

P.s. this is not just for Twitter auth, but for ANY async function. I had to use Meteor.bindEnvironment() to store the result from an async function which I needed to write to the database by making a Meteor method call. It worked for that as well.

Hope this helps someone in need!

这篇关于Meteor 检索 Twitter 凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆