Created
July 9, 2012 15:17
-
-
Save pmoelgaard/3077111 to your computer and use it in GitHub Desktop.
CommonJS Module Client for the Podio API
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 Podio = require('./podio'); | |
Podio.getAccessToken(function(accessToken) { | |
deviceInfoList.forEach( function( deviceInfo ) { | |
Podio.search( deviceInfo.properties[ 0 ].value, accessToken, function( searchResult ) { | |
console.log( searchResult ); | |
}); | |
}); | |
}); |
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
function getAccessToken(callback) { | |
var https = require('https'); | |
var getAccessTokenOptions = { | |
host : 'podio.com', | |
path : '/oauth/token?grant_type=password&username=[USERNAME]&password=[PASSWORD]&client_id=[CLIENT_ID]&redirect_uri=[REDIRECT_URI]&client_secret=[CLIENT_SECRET]', | |
port : 443, | |
method : 'POST', | |
headers : { | |
'Content-Length' : 0 | |
} | |
}; | |
var getAccessTokenReq = https.request(getAccessTokenOptions, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function(getAccessTokenData) { | |
var accessToken = JSON.parse(getAccessTokenData).access_token; | |
console.log('Token: ' + accessToken); | |
callback(accessToken); | |
}); | |
}); | |
getAccessTokenReq.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
getAccessTokenReq.end(); | |
}; | |
function search(text, accessToken, callback) { | |
var query = { | |
query : text | |
}; | |
var queryString = JSON.stringify(query); | |
var options = { | |
host : "api.podio.com", | |
path : '/search/', | |
port : 443, | |
method : "POST", | |
headers : { | |
'Authorization' : "OAuth2 " + accessToken, | |
'Content-Length' : Buffer.byteLength(queryString, 'utf8') | |
} | |
}; | |
console.log( "Searching for "+ queryString ); | |
var https = require('https'); | |
var req = https.request(options, function(result) { | |
result.setEncoding('utf8'); | |
result.on('data', function(data) { | |
var result = JSON.parse(data); | |
callback(result); | |
}); | |
console.log('STATUS: ' + result.statusCode); | |
}); | |
req.on('error', function(e) { | |
console.log('problem with update request: ' + e.message); | |
}); | |
req.write(queryString); | |
req.end(); | |
} | |
exports.getAccessToken = getAccessToken; | |
exports.search = search; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment