【问题标题】:215 error while requesting Token from Twitter oAuth API从 Twitter oAuth API 请求令牌时出现 215 错误
【发布时间】:2018-12-27 06:19:47
【问题描述】:

我正在尝试在我的 NodeJS 应用程序中实现 Twitter 登录。

根据来自 Twitter 的 documentation,我需要通过 POST 请求将以下参数传递给此 url。 网址:

https://api.twitter.com/oauth/request_token

参数:

 oauth_nonce=, 
 oauth_signature=, 
 oauth_callback="http%3A%2F%2Fmyapp.com%3A3005%2Ftwitter%2Fprocess_callback", 
 oauth_signature_method="HMAC-SHA1",       
 oauth_timestamp="currentTimestamp", 
 oauth_consumer_key=“myKEY”, 
 oauth_version="1.0"

我正在为oauth_nonce 参数传递一个随机字符串。我不清楚如何创建oauth_signature

每当我发出 POST 请求时,我都会不断收到 215 错误,因为我猜是因为 oauth_nonceoauth_signature 值不正确。

如何在 NodeJS 中发出请求时生成 oauth_nonceoauth_signature 值。

【问题讨论】:

  • 到现在还没有进展?

标签: node.js twitter twitter-oauth


【解决方案1】:

无法评论,所以在这里回答。 docusais,oauth_nonce

是您的应用程序应为每个唯一请求生成的唯一令牌

它就像加密的盐。您可以在同一链接下找到oauth_signature。如何生成它描述了here

【讨论】:

    【解决方案2】:

    这些参数需要在你的授权头中传递:

    OAuth oauth_nonce="K7ny27JTpKVsTgdyLdDfmQQWVLERj2zAK5BslRsqyw", 
    oauth_callback="http%3A%2F%2Fmyapp.com%3A3005%2Ftwitter%2Fprocess_callback", 
    oauth_signature_method="HMAC-SHA1", 
    oauth_timestamp="1300228849", 
    oauth_consumer_key="OqEqJeafRSF11jBMStrZz", 
    oauth_signature="Pc%2BMLdv028fxCErFyi8KXFM%2BddU%3D", 
    oauth_version="1.0"
    

    但在此之前,您需要获得一个签名,然后它将为您提供上述所有参数。检查the documentation here

    建议使用像 passport 这样的第三方库,如果需要,它可以大大简化此过程。

    【解决方案3】:

    回答你的问题:

    如何在 NodeJS 中发出请求时生成 oauth_nonce 和 oauth_signature 值。

    阅读这些帖子将对Creating a signatureHow to generate an OAuth nonce 有所帮助。

    如果使用得当,Twitter 会很有意义。我看过一些对我周围的人非常有用的教程,我觉得你可能想查看Node Authentication: TwitterImplementing Sign in with Twitter for Node.js

    或者您可能想查看passportjs

    您必须通过运行以下命令将其安装到您的项目中: npm install passport-twitter

    配置

    const passport = require('passport')
      , TwitterStrategy = require('passport-twitter').Strategy;
    
    passport.use(new TwitterStrategy({
        consumerKey: TWITTER_CONSUMER_KEY,
        consumerSecret: TWITTER_CONSUMER_SECRET,
        callbackURL: "http://www.example.com/auth/twitter/callback"
      },
      function(token, tokenSecret, profile, done) {
        User.findOrCreate(..., function(err, user) {
          if (err) { return done(err); }
          done(null, user);
        });
      }
    ));
    

    这个Github repo 可能对你很有参考价值。

    如果你使用 reactjs,这个 (npm i react-twitter-auth) 可能对你也有用。

    【讨论】:

      【解决方案4】:

      最后我可以使用this库获得访问令牌

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-04
        • 2018-10-28
        • 1970-01-01
        • 1970-01-01
        • 2012-06-22
        • 2019-09-16
        • 2011-05-04
        • 2021-03-22
        相关资源
        最近更新 更多