일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 글자뒤집기
- 웹퍼블리셔
- 자바스크립트
- 홈페이지제작
- 무료호스팅
- 무료서버
- postcodify
- php
- 호스팅
- 마우스오른쪽
- 무료제작
- 무료사이트
- 무료홈페이지
- 무료 호스팅
- 마우스 오른쪽 버튼
- 웹퍼블리싱
- 우편번호 API
- 무료 홈페이지 제작
- 복사방지
- 웹호스팅
- 무료
- 클릭해제
- 무료 웹 호스팅
- 새우편번호
- 자바키워드
- php호스팅
- 퍼빌리셔
- 문자열 뒤집기
- 우체국 우편번호
- 무료 홈페이지
- Today
- Total
Developer Factory
Nody.js 본문
node-twitter-api
Simple module for using Twitter's API in node.js
npm install node-twitter-api
9 | downloads in the last day |
159 | downloads in the last week |
515 | downloads in the last month |
Maintainers | |
---|---|
Version | 1.1.3 last updated 2 months ago |
License | MIT |
Keywords | twitter, oauth, rest, streaming |
Repository | https://github.com/reneraab/node-twitter-api.git (git) |
Homepage | https://github.com/reneraab/node-twitter-api |
Bugs | https://github.com/reneraab/node-twitter-api/issues |
Dependencies | oauth |
Dependents | gulp-twitter |
node-twitter
Simple module for using Twitter's API in node.js
Installation
npm install node-twitter-api
Usage
Step 1: Initialization
var twitterAPI = require('node-twitter-api');var twitter = new twitterAPI({
consumerKey: 'your consumer Key',
consumerSecret: 'your consumer secret',
callback: 'http://yoururl.tld/something'});
Step 2: Getting a request token
twitter.getRequestToken(function(error, requestToken, requestTokenSecret, results){
if (error) {
console.log("Error getting OAuth request token : " + error);
} else {
//store token and tokenSecret somewhere, you'll need them later; redirect user
}});
If no error has occured, you now have a requestToken
and a requestTokenSecret
. You should store them somewhere (e.g. in a session, if you are using express), because you will need them later to get the current user's access token, which is used for authentification.
Step 3: Getting an Access Token
Redirect the user to https://twitter.com/oauth/authenticate?oauth_token=[requestToken]
. If he allows your app to access his data, Twitter will redirect him to your callback-URL (defined in Step 1) containing the get-parameters: oauth_token
and oauth_verifier
. You can use oauth_token
(which is the requestToken
in Step 2) to find the associated requestTokenSecret
. You will need requestToken
, requestTokenSecret
andoauth_verifier
to get an Access Token.
twitter.getAccessToken(requestToken, requestTokenSecret, oauth_verifier, function(error, accessToken, accessTokenSecret, results) {
if (error) {
console.log(error);
} else {
//store accessToken and accessTokenSecret somewhere (associated to the user)
//Step 4: Verify Credentials belongs here
}});
If no error occured, you now have an accessToken
and an accessTokenSecret
. You need them to authenticate later API-calls.
Step 4: (Optional) Verify Credentials
twitter.verifyCredentials(accessToken, accessTokenSecret, function(error, data, response) {
if (error) {
//something was wrong with either accessToken or accessTokenSecret
//start over with Step 1
} else {
//accessToken and accessTokenSecret can now be used to make api-calls (not yet implemented)
//data contains the user-data described in the official Twitter-API-docs
//you could e.g. display his screen_name
console.log(data["screen_name"]);
}});
Methods
(Allmost) all function names replicate the endpoints of the Twitter API 1.1. If you want to post a status e. g. - which is done by posting data to statuses/update - you can just do the following:
twitter.statuses("update", {
status: "Hello world!"
},
accessToken,
accessTokenSecret,
function(error, data, response) {
if (error) {
// something went wrong
} else {
// data contains the data sent by twitter
}
});
Most of the functions use the scheme: twitter.[namespace]([type], [params], [accessToken], [accessTokenSecret], [callback]);
- namespace is the word before the slash (e.g. "statuses", "search", "direct_messages" etc.)
- type is the word after the slash (e.g. "create", "update", "show" etc.)
- params is an object containing the parameters you want to give to twitter (refer to the Twitter API Documentation for more information)
- accessToken and accessTokenSecret are the token and secret of the authenticated user
- callback is a function with the parameters error (either null or an error object), data (data object) andresponse (unprocessed response from Twitter)
AN EXAMPLE: WEBSERVER
This simple web server written in Node responds with "Hello World" for every request.
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');}).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/');
To run the server, put the code into a file example.js
and execute it with thenode
program from the command line:
% node example.js Server running at http://127.0.0.1:1337/
Here is an example of a simple TCP server which listens on port 1337 and echoes whatever you send it:
var net = require('net'); var server = net.createServer(function (socket) { socket.write('Echo server\r\n'); socket.pipe(socket);}); server.listen(1337, '127.0.0.1');
'Developer > Node.js' 카테고리의 다른 글
REST API (0) | 2014.06.24 |
---|---|
그림으로 보는 node-twitter-api (0) | 2014.06.23 |
node-twitter-api (0) | 2014.06.23 |
twitter node (0) | 2014.06.23 |