【问题标题】:Twitter authentication in Codebird JSCodebird JS 中的 Twitter 身份验证
【发布时间】:2016-02-23 17:27:44
【问题描述】:

我对将社交网站集成到网站非常陌生。我有点设法整合 Facebook,但我不知道如何整合 Twitter。

我想通过 Twitter 帐户登录,然后从 Twitter 获取用户名和一些其他数据。我有一个消费者密钥和消费者秘密。我不知道如何从这里开始,而且我的 Google 搜索到目前为止也没有帮助。

我正在尝试使用 codebird js:

$(function() {
    $('#twitter').click(function(e) {
        e.preventDefault();
        var cb = new Codebird;
        cb.setConsumerKey("redacted", "redacted");
        cb.__call(
            "oauth_requestToken",
            { oauth_callback: "http://127.0.0.1:49479/" },
            function (reply, rate, err) {
                if (err) {
                    console.log("error response or timeout exceeded" + err.error);
                }
                if (reply) {
                    // stores it
                    cb.setToken(reply.oauth_token, reply.oauth_token_secret);

                    // gets the authorize screen URL
                    cb.__call(
                        "oauth_authorize",
                        {},
                        function (auth_url) {
                            window.codebird_auth = window.open(auth_url);
                        }
                    );
                }
            }
        );
        cb.__call(
            "account_verifyCredentials",
            {},
            function(reply) {
                console.log(reply);
            }
        );                 
    })
});

但我明白了

您的凭据不允许访问此资源

如何解决这个问题并获取用户数据?我愿意使用替代的 Twitter 实现。

【问题讨论】:

  • 本地主机上的 OAuth 回调 - 这将如何工作?还是仅用于此处的代码复制?
  • localhost 很好 - Twitter 会很高兴地重定向到任何 URI,并且 OP 可能在那里设置了开发页面。
  • 请从帖子中删除您的 Twitter API 使用者密钥和秘密,然后在 apps.twitter.com 上重新生成它们。它们是保密的。

标签: javascript jquery facebook twitter


【解决方案1】:

你不能在那里打电话给cb._call( "account_verifyCredentials"...

该代码只有一个请求令牌,而不是访问令牌,只有在用户授权您的应用后(在 Twitter 身份验证弹出窗口上),您才会收到该令牌。

您正在使用“没有 PIN 的回调 URL”方法,as documented on the README. 所以您需要在您的 http://127.0.0.1:49479/ 页面上实现该示例代码。

此外,这基本上要求您将 oauth 凭据存储在某处。在下面的示例中,我使用了localStorage

$(function () {
  $('#twitter').click(function (e) {
    e.preventDefault();
    var cb = new Codebird;
    cb.setConsumerKey("CeDhZjVa0d8W02gWuflPWQmmo", "YO4RI2UoinJ95sonHGnxtYt4XFtlAhIEyt89oJ8ZajClOyZhka");

    var oauth_token = localStorage.getItem("oauth_token");
    var oauth_token_secret = localStorage.getItem("oauth_token_secret");
    if (oauth_token && oauth_token_secret) {
      cb.setToken(oauth_token, oauth_token_secret);
    } else {
      cb.__call(
        "oauth_requestToken", {
          oauth_callback: "http://127.0.0.1:49479/"
        },
        function (reply, rate, err) {
          if (err) {
            console.log("error response or timeout exceeded" + err.error);
          }
          if (reply) {
            console.log("reply", reply)
              // stores it
            cb.setToken(reply.oauth_token, reply.oauth_token_secret);

            // save the token for the redirect (after user authorizes)
            // we'll want to compare these values 
            localStorage.setItem("oauth_token", reply.oauth_token);
            localStorage.setItem("oauth_token_secret", reply.oauth_token_secret);

            // gets the authorize screen URL
            cb.__call(
              "oauth_authorize", {},
              function (auth_url) {
                console.log("auth_url", auth_url);
                // JSFiddle doesn't open windows:
                // window.open(auth_url);
                $("#authorize").attr("href", auth_url);

                // after user authorizes, user will be redirected to
                // http://127.0.0.1:49479/?oauth_token=[some_token]&oauth_verifier=[some_verifier]
                // then follow this section for coding that page:
                // https://github.com/jublonet/codebird-js#authenticating-using-a-callback-url-without-pin
              });
          }
        });
    }
  })
});

还发了JSFiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 2011-10-05
    • 2011-06-18
    • 2013-04-11
    • 2011-04-25
    • 2011-06-10
    相关资源
    最近更新 更多