百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

codebird-js

> 安全
开源

JavaScript 中的 Twitter 库。

382 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

JavaScript 中的 Twitter 库。

codebird-js

A Twitter library in JavaScript.

Copyright (C) 2010-2018 Jublo Limited

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see .

Including Codebird

To include Codebird in your code, add its scripts to your markup:

xml

You may also use a JavaScript module loader of your choice (such as RequireJS or the one bundled in Node.js) to load Codebird unobtrusively. In Node.js, loading Codebird looks like this:

javascript
var Codebird = require("codebird");
// or with leading "./", if the codebird.js file is in your main folder:
// var Codebird = require("./codebird");

var cb = new Codebird();
cb.setConsumerKey("YOURKEY", "YOURSECRET");

Authentication

To authenticate your API requests on behalf of a certain Twitter user (following OAuth 1.0a), take a look at these steps:

xml

You may either set the OAuth token and secret, if you already have them:

javascript
cb.setToken("YOURTOKEN", "YOURTOKENSECRET");

Or you authenticate, like this:

…

:warning: Codebird server calls do not always go through when being processed in a hyperlink onclick handler. Be sure to cancel the default procedure before calling Codebird, like this (jQuery):

javascript
$(function() {

    $('#auth').click(function(e) {
        e.preventDefault();

        var cb = new Codebird;
// ...

Now you need to add a PIN box to your website. After the user enters the PIN, complete the authentication:

javascript
cb.__call(
  "oauth_accessToken",
  { oauth_verifier: document.getElementById("PINFIELD").value },
  function(reply, rate, err) {
    if (err) {
      console.log("error response or timeout exceeded" + err.error);
    }
    if (reply) {
      // store the authenticated token, which may be different from the request token (!)
      cb.setToken(reply.oauth_token, reply.oauth_token_secret);
    }

    // if you need to persist the login after page reload,
    // consider storing the token in a cookie or HTML5 local storage
  }
);

Logging out

In case you want to log out the current user (to log in a different user without creating a new Codebird object), just call the logout() method.

javascript
cb.logout().then(() => {
  // user is now logged out
});

Codebird also supports calling the oauth/invalidate_token method directly:

cb.__call("oauth_invalidateToken", {
  access_key:        "1234",
  access_key_secret: "5678"
}).then(() => {
  // tokens are now reset
});

Application-only auth

Some API methods also support authenticating on a per-application level. This is useful for getting data that are not directly related to a specific Twitter user, but generic to the Twitter ecosystem (such as search/tweets).

To obtain an app-only bearer token, call the appropriate API:

javascript
cb.__call("oauth2_token", {}, function(reply, err) {
  var bearer_token;
  if (err) {
    console.log("error response or timeout exceeded" + err.error);
  }
  if (reply) {
    bearer_token = reply.access_token;
  }
});

I strongly recommend that you store the obtained bearer token in your database. There is no need to re-obtain the token with each page load, as it becomes invalid only when you call the oauth2/invalidate_token method.

If you already have your token, tell Codebird to use it:

javascript
cb.setBearerToken("YOURBEARERTOKEN");

In this case, you don't need to set the consumer key and secret. For sending an API request with app-only auth, see the ‘Usage examples’ section.

Authenticating using a callback URL, without PIN

  1. Before sending your user off to Twitter, you have to store the request token and its secret, for example in a cookie.
  2. In the callback URL, extract those values and assign them to the Codebird object.
  3. Extract the oauth_verifier field from the request URI.

In Javascript, try extracting the URL parameter like this:

javascript
var cb = new Codebird();
var current_url = location.toString();
var query = current_url.match(/\?(.+)$/).split("&amp;");
var parameters = {};
var parameter;

cb.setConsumerKey("STUFF", "HERE");

for (var i = 0; i  0) {
  cb.__call("followers_list", { cursor: nextCursor }, function(result2) {
    // ...
  });
}

To navigate back instead of forth, use the field resultX.previous_cursor_str instead of next_cursor_str.

It might make sense to use the cursors in a loop. Watch out, though, not to send more than the allowed number of requests to followers/list per rate-limit timeframe, or else you will hit your rate-limit.

…use xAuth with Codebird?

Codebird supports xAuth just like every other authentication used at Twitter. Remember that your application needs to be whitelisted to be able to use xAuth.

Here’s an example:

javascript
cb.__call(
  "oauth_accessToken",
  {
    x_auth_username: "username",
    x_auth_password: "4h3_p4$$w0rd",
    x_auth_mode: "client_auth"
  },
  function(reply) {
    console.log(reply);
    // ...
  }
);

If everything went fine, you will get an object like this:

javascript
{
    "oauth_token": "14648265-ABLfBFlE*********************************",
    "oauth_token_secret": "9yTBY3pEfj*********************************",
    "user_id": "14648265",
    "screen_name": "jublonet",
    "x_auth_expires": "0",
    "httpstatus": 200
}

Are you getting a strange error message, an empty error, or status "0"? If the user is enrolled in login verification, the server will return a HTTP 401 error with a custom body (that may be filtered by your browser).

You may check the browser web console for an error message.

When this error occurs, advise the user to generate a temporary password on twitter.com and use that to complete signing in to the application.

…access the Collections API?

Collections are a type of timeline that you control and can be hand curated and/or programmed using an API.

Pay close attention to the differences in how collections are presented — often they will be decomposed, efficient objects with information about users, Tweets, and timelines grouped, simplified, and stripped of unnecessary repetition.

Never care about the OAuth signing specialities and the JSON POST body for POST and PUT calls to these special APIs. Codebird takes off the work for you and will always send the correct Content-Type automatically.

Find out more about the Collections API in the Twitter API docs. More information on the Direct Messages API and the Account Activity API is available there as well.

Here’s a sample for adding a Tweet using the Collections API:

javascript
cb.__call(
  "collections_entries_curate",
  {
    id: "custom-672852634622144512",
    changes: [{ op: "add", tweet_id: "672727928262828032" }]
  },
  function(reply, rate) {
    document.body.innerText = JSON.stringify(reply);
  }
);

…use promises instead of callback functions?

Have you ever heard of the Pyramid of Doom? It’s when code progresses more to the right because of excessive nesting than it progresses from top to bottom.

Because of the asynchronous requests, Codebird will use callbacks that you provide. They are called when the result from the Twitter API has arrived. However, to streamline code, there is a sleeker concept for this: Promises.

There are several popular libraries that support promises. Codebird will auto-detect and use any of the following:

  • jQuery Deferred
  • Q
  • RSVP
  • when

Here’s a usage sample for promises:

javascript
cb.__call("statuses_update", { status: "Whohoo, I just tweeted!" }).then(
  function(data) {
    var reply = data.reply,
      rate = data.rate;
    // ...
  },
  function(err) {
    // ...
  }
);

Since the app-only flag is the fourth parameter for __call, you’ll have to provide a callback stub nonetheless even with promises:

javascript
cb.__call(
  "search_tweets",
  { q: "#PHP7" },
  null, // no callback needed, we have the promise
  true // app-only auth
).then(
  function(data) {
    var reply = data.reply,
      rate = data.rate;
    // ...
  },
  function(err) {
    // ...
  }
);

Tips:

  • If you provide both (callback and promise.then), Codebird will first call the callback, then resolve the promise.

  • If the request fails due to any errors, Codebird will reject the promise.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptcodebirdcodebird-jscors-proxyjavascript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类安全
定价开源

> 相关工具

O
OWASP ZAP
开源 Web 应用安全扫描器
O
owasp-wstg-tracker
Simple web app to track OWASP WSTG security testing progress
H
homebridge-mi-gateway-security
XiaoMi Gateway Security plugin for HomeBridge.