Created
July 27, 2012 10:19
-
-
Save dongyuwei/3187284 to your computer and use it in GitHub Desktop.
sina weibo oauth2 api,in nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var https = require('https'); | |
var path = require('path'); | |
var fs = require('fs'); | |
var qs = require('querystring'); | |
var express = require('express'); | |
var multiparter = require("multiparter"); | |
process.on('uncaughtException', function(err) { | |
console.error('Caught exception: ', err); | |
}); | |
function parse_json(res, callback) { | |
var list = []; | |
res.on('data', function(chunk) { | |
list.push(chunk); | |
}); | |
res.on('end', function() { | |
callback(JSON.parse(Buffer.concat(list).toString())); | |
list = null; | |
}); | |
res.on("error", function(error) { | |
console.log(error); | |
}); | |
}; | |
IMG_CONTENT_TYPES = { | |
'.gif': 'image/gif', | |
'.jpeg': 'image/jpeg', | |
'.jpg': 'image/jpeg', | |
'.png': 'image/png' | |
} | |
exports.config = function(client_id, client_secret, redirect_uri) { | |
this.client_id = client_id; | |
this.client_secret = client_secret; | |
this.redirect_uri = redirect_uri; | |
return this; | |
}; | |
exports.get_authorize_url = function(client_id, redirect_uri) { | |
return 'https://api.weibo.com/oauth2/authorize?client_id=' + this.client_id + '&redirect_uri=' + this.redirect_uri + '&response_type=code'; | |
}; | |
exports.access_token = function(code, callback) { | |
var post_data = qs.stringify({ | |
'client_id' : this.client_id, | |
'client_secret' : this.client_secret, | |
'grant_type' : 'authorization_code', | |
'code' : code, | |
'redirect_uri' : this.redirect_uri | |
}); | |
var options = { | |
host : 'api.weibo.com', | |
path : '/oauth2/access_token', | |
method : 'POST', | |
headers : { | |
'Content-Type' : 'application/x-www-form-urlencoded', | |
'Content-Length' : post_data.length | |
} | |
}; | |
var req = https.request(options, function(res) { | |
parse_json(res, callback); | |
}); | |
req.write(post_data) | |
req.end(); | |
req.on('error', function(e) { | |
console.error(e); | |
}); | |
}; | |
exports.statuses = { | |
update : function(status, access_token, callback) { | |
var post_data = qs.stringify({ | |
'status' : status, | |
'access_token' : access_token | |
}); | |
var options = { | |
host : 'api.weibo.com', | |
path : '/2/statuses/update.json', | |
method : 'POST', | |
headers : { | |
'Content-Type' : 'application/x-www-form-urlencoded', | |
'Content-Length' : post_data.length | |
} | |
}; | |
var req = https.request(options, function(res) { | |
parse_json(res, callback); | |
}); | |
req.write(post_data) | |
req.end(); | |
req.on('error', function(e) { | |
console.error(e); | |
}); | |
}, | |
upload : function(status, pic_url, access_token,callback) { | |
var request = new multiparter.request(https, { | |
host : 'api.weibo.com', | |
port : '443', | |
path : "/2/statuses/upload.json", | |
method : "POST", | |
headers:{//上传文件时使用Authorization header做授权认证 | |
'Authorization':'OAuth2 ' + access_token | |
} | |
}); | |
request.setParam('status', status); | |
fs.stat(pic_url, function(err, stats) { | |
if(err) { | |
throw err; | |
} | |
request.addStream('pic', path.basename(pic_url), IMG_CONTENT_TYPES[path.extname(pic_url)], stats.size, fs.createReadStream(pic_url)); | |
request.send(function(error, res) { | |
if (error) { | |
console.log(error); | |
} | |
parse_json(res,callback); | |
}); | |
}); | |
}, | |
friends_timeline : function(access_token, callback) { | |
https.get({ | |
host : 'api.weibo.com', | |
path : '/2/statuses/friends_timeline.json?access_token=' + access_token | |
}, function(res) { | |
parse_json(res, callback); | |
}).on('error', function(e) { | |
console.error(e); | |
}); | |
} | |
}; | |
//start test-------------------- | |
var app = express.createServer(); | |
app.use(app.router); | |
app.get('/', function(req, res) { | |
exports.config('app key', 'secret', 'http://mail2weibo.session.im/callback'); | |
res.write('<a href="URI">使用新浪微博授权</a>'.replace('URI', exports.get_authorize_url()), 'utf-8'); | |
res.end(); | |
}); | |
app.get('/callback', function(req, res) { | |
var code = req.param('code', null); | |
if(code) { | |
exports.access_token(code, function(data) { | |
console.log(data); | |
res.end('auth ok '); | |
// exports.statuses.upload('oauth2 nodejs','/Users/ilfe/code/mail2http/test/face.jpeg',data['access_token'], function(json) { | |
// console.log(json); | |
// }); | |
// exports.statuses.update('test', data['access_token'], function(json){ | |
// console.log(json); | |
// }); | |
// exports.statuses.friends_timeline(data['access_token'], function(json){ | |
// console.log(json); | |
// }); | |
}); | |
} | |
}); | |
app.listen(80); | |
//end test-------------------- |
依赖express,及multiparter(发布带图微博用).
npm install express multiparter
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
除oauth2授权认证外,目前仅实现了我需要的friends_timeline, update,upload 3个api.