node.js 的 Twitter 应用程序 OAuth

Twitter app OAuth with node.js

下面的 "should" 工作到 post 推文 message 下面的内容,即 "Lorem ipsum dolor..."

var OAuth = require("oauth").OAuth;
const twitterer = new OAuth(    "https://api.twitter.com/oauth/request_token",
                                "https://api.twitter.com/oauth/access_token",
                                getContext().configuration.tasks.auto_tweet.apiAccessKey,
                                getContext().configuration.tasks.auto_tweet.apiAccessSecret,
                                // getContext().configuration.tasks.auto_tweet.apiPostKey,
                                // getContext().configuration.tasks.auto_tweet.apiPostSecret,
                                "1.0", null,"HMAC-SHA1");
// ... business logic
const message = "Lorem ipsum dolor..."
twitterer.post(     
    "https://api.twitter.com/1.1/statuses/update.json",
    getContext().configuration.tasks.auto_tweet.apiPostKey,
    getContext().configuration.tasks.auto_tweet.apiPostSecret,
                                        ({'status': message}),
                                        "application/json",
                                        function (error, data, response2) {
                                            if(error){
                                                failedTweets ++;
                                                console.log("~~~~~~~~ Failed to send" , failedTweets, "tweets" );
                                                console.log(error);
                                            }
                                            else{ sentTweets ++; }
                                            console.log("~~~~~~~~ Sent" , sentTweets, "tweets" );
                                            setTimeout(function(){
                                                sendNextTweet();
                                            },1000);

上面的结果,在 Twitter 应用仪表板中填充的键是这个错误:

`{ statusCode: 401, data: '{"errors":[{"code":89,"message":"Invalid or expired token."}]}' }`

我在这里不知所措——这是一个需要 post 以自己的专用用户身份发推特的应用程序,它没有验证其他用户或任何类似的东西,所以我相信 callback_url 是无关紧要的......而且无论如何我都不知道 callback_url 如果需要的话会是什么。

此外,应用程序设置页面上的 'Test OAuth' 按钮 (https://apps.twitter.com/app/XXXXX/settings) yields a page with this message "Sorry, that page doesn’t exist!" on it at https://dev.twitter.com/apps/XXXXX/oauth。不清楚这告诉我什么,但是

发件人:https://dev.twitter.com/oauth/application-only

With Application-only authentication you don’t have the context of an authenticated user and this means that any request to API for endpoints that require user context, such as posting tweets, will not work. However, the set of endpoints that will still be available can have a higher rate limit.

And it won’t be able to:

Post tweets or other resources;

我建议您尝试使用 Twitter 的节点包: 例如 https://www.npmjs.com/package/twitter

事实证明,第一个要点是一个谎言。当应用程序没有重新加载交换的配置时,我很可能 "tested"。问题是 accessKey 应该在 post 中使用,而 "post" 键(实际上是消费者键)是 OAuth 构造函数中的相关值。