Updated the Oauth2 code to work with the new https api

This commit is contained in:
ciaranj 2011-02-06 19:52:44 +00:00
parent 5041ca168a
commit 349d258b1f

View File

@ -1,10 +1,8 @@
var querystring= require('querystring'), var querystring= require('querystring'),
crypto= require('crypto'), crypto= require('crypto'),
http= require('http'), https= require('https'),
URL= require('url'); URL= require('url');
var sys= require('sys');
exports.OAuth2= function(clientId, clientSecret, baseSite, authorizePath, accessTokenPath) { exports.OAuth2= function(clientId, clientSecret, baseSite, authorizePath, accessTokenPath) {
this._clientId= clientId; this._clientId= clientId;
this._clientSecret= clientSecret; this._clientSecret= clientSecret;
@ -29,7 +27,6 @@ exports.OAuth2.prototype._request= function(method, url, headers, access_token,
var creds = crypto.createCredentials({ }); var creds = crypto.createCredentials({ });
var parsedUrl= URL.parse( url, true ); var parsedUrl= URL.parse( url, true );
if( parsedUrl.protocol == "https:" && !parsedUrl.port ) parsedUrl.port= 443; if( parsedUrl.protocol == "https:" && !parsedUrl.port ) parsedUrl.port= 443;
var httpClient = http.createClient(parsedUrl.port, parsedUrl.hostname, true, creds);
var realHeaders= {}; var realHeaders= {};
if( headers ) { if( headers ) {
@ -46,17 +43,17 @@ exports.OAuth2.prototype._request= function(method, url, headers, access_token,
parsedUrl.query["access_token"]= access_token; parsedUrl.query["access_token"]= access_token;
} }
var request = httpClient.request(method, parsedUrl.pathname + "?" + querystring.stringify(parsedUrl.query), realHeaders );
httpClient.addListener("secure", function () {
/* // disable verification for now.
var verified = httpClient.verifyPeer();
if(!verified) this.end(); */
});
var result= ""; var result= "";
request.addListener('response', function (response) {
var options = {
host:parsedUrl.hostname,
port: parsedUrl.port,
path: parsedUrl.pathname + "?" + querystring.stringify(parsedUrl.query),
method: method,
headers: realHeaders
};
request = https.request(options, function (response) {
response.addListener("data", function (chunk) { response.addListener("data", function (chunk) {
result+= chunk result+= chunk
}); });
@ -69,6 +66,10 @@ var verified = httpClient.verifyPeer();
}); });
}); });
request.on('error', function(e) {
callback(e);
});
request.end(); request.end();
} }