Aru*_*mar 10 javascript twitter jquery facebook
我很擅长将社交网站整合到网站中.我有点设法整合Facebook,但我不知道如何整合Twitter.
我想通过Twitter帐户登录,然后从Twitter获取用户名和其他一些数据.我有消费者密钥和消费者秘密.我不确定如何从这里开始,到目前为止我的谷歌搜索没有帮助.
我正在尝试使用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);
}
);
})
});
Run Code Online (Sandbox Code Playgroud)
但我明白了
您的凭据不允许访问此资源
如何解决此问题并获取用户数据?我愿意使用备用的Twitter实现.
你不能在那里打电话cb._call( "account_verifyCredentials"...。
该代码只有一个请求令牌,而不是一个访问令牌,您只有在用户授权您的应用程序(在 Twitter 身份验证弹出窗口上)后才会收到该令牌。
您正在使用“无 PIN 的回调 URL”方法,如自述文件中所述。 因此,您需要在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
});
}
});
}
})
});
Run Code Online (Sandbox Code Playgroud)
还做了一个JSFiddle
| 归档时间: |
|
| 查看次数: |
1312 次 |
| 最近记录: |