codebird-js
Warning
This project is no longer maintained. Codebird was written for the Twitter API v1.1,
which X has retired or put behind paid tiers since 2023. Most methods here no longer work,
and no fixes or releases are planned. For new projects, use a library that targets the
current X API v2.
Thanks to everyone who used and contributed to Codebird from 2010 on.
A Twitter library in JavaScript.
Copyright (C) 2010-2018 Jublo Limited support@jublo.net
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 http://www.gnu.org/licenses/.
Including Codebird
To include Codebird in your code, add its scripts to your markup:
<script type="text/javascript" src="codebird.js"></script>
<script type="text/javascript">
var cb = new Codebird;
cb.setConsumerKey("YOURKEY", "YOURSECRET");
</script>
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:
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:
<script type="text/javascript" src="codebird.js"></script>
<script type="text/javascript">
var cb = new Codebird;
cb.setConsumerKey("YOURKEY", "YOURSECRET");
</script>
You may either set the OAuth token and secret, if you already have them:
cb.setToken("YOURTOKEN", "YOURTOKENSECRET");
Or you authenticate, like this:
// gets a request token
cb.__call("oauth_requestToken", { oauth_callback: "oob" }, function(
reply,
rate,
err
) {
if (err) {
console.log("error response or timeout exceeded" + err.error);
}
if (reply) {
if (reply.errors && reply.errors["415"]) {
// check your callback URL
console.log(reply.errors["415"]);
return;
}
// stores the token
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);
});
}
});
⚠️ 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):
$(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:
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.
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:
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:
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
- Before sending your user off to Twitter, you have to store the request token and its secret, for example in a cookie.
- In the callback URL, extract those values and assign them to the Codebird object.
- Extract the
oauth_verifier field from the request URI.
In Javascript, try extracting the URL parameter like this:
var cb = new Codebird();
var current_url = location.toString();
var query = current_url.match(/\?(.+)$/).split("&");
var parameters = {};
var parameter;
cb.setConsumerKey("STUFF", "HERE");
for (var i = 0; i < query.length; i++) {
parameter = query[i].split("=");
if (parameter.length === 1) {
parameter[1] = "";
}
parameters[decodeURIComponent(parameter[0])] = decodeURIComponent(
parameter[1]
);
}
// check if oauth_verifier is set
if (typeof parameters.oauth_verifier !== "undefined") {
// assign stored request token parameters to codebird here
// ...
cb.setToken(
stored_somewhere.oauth_token,
stored_somewhere.oauth_token_secret
);
cb.__call(
"oauth_accessToken",
{
oauth_verifier: parameters.oauth_verifier
},
function(reply, rate, err) {
if (err) {
console.log("error response or timeout exceeded" + err.error);
}
if (reply) {
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
}
);
}
Usage examples
⚠️ Because the Consumer Key and Token Secret are available in the code,
it is important that you configure your app as read-only at Twitter,
unless you are sure to know what you are doing.
When you have an access token, calling the API is simple: