Guest User

Untitled

a guest
Nov 22nd, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. function getAccessToken(callback) {
  2.  
  3. var https = require('https');
  4.  
  5. var getAccessTokenOptions = {
  6. host : 'podio.com',
  7. path : '/oauth/token?grant_type=password&username=[USERNAME]&password=[PASSWORD]&client_id=[CLIENT_ID]&redirect_uri=[REDIRECT_URI]&client_secret=[CLIENT_SECRET]',
  8. port : 443,
  9. method : 'POST',
  10. headers : {
  11. 'Content-Length' : 0
  12. }
  13. };
  14. var getAccessTokenReq = https.request(getAccessTokenOptions, function(res) {
  15.  
  16. res.setEncoding('utf8');
  17.  
  18. res.on('data', function(getAccessTokenData) {
  19.  
  20. var accessToken = JSON.parse(getAccessTokenData).access_token;
  21. console.log('Token: ' + accessToken);
  22.  
  23. callback(accessToken);
  24. });
  25. });
  26.  
  27. getAccessTokenReq.on('error', function(e) {
  28. console.log('problem with request: ' + e.message);
  29. });
  30.  
  31. getAccessTokenReq.end();
  32. };
  33.  
  34. function search(text, accessToken, callback) {
  35.  
  36. var query = {
  37. query : text
  38. };
  39. var queryString = JSON.stringify(query);
  40.  
  41. var options = {
  42. host : "api.podio.com",
  43. path : '/search/',
  44. port : 443,
  45. method : "POST",
  46. headers : {
  47. 'Authorization' : "OAuth2 " + accessToken,
  48. 'Content-Length' : Buffer.byteLength(queryString, 'utf8')
  49. }
  50. };
  51.  
  52. console.log( "Searching for "+ queryString );
  53.  
  54. var https = require('https');
  55. var req = https.request(options, function(result) {
  56.  
  57. result.setEncoding('utf8');
  58.  
  59. result.on('data', function(data) {
  60.  
  61. var result = JSON.parse(data);
  62. callback(result);
  63. });
  64. console.log('STATUS: ' + result.statusCode);
  65. });
  66.  
  67. req.on('error', function(e) {
  68. console.log('problem with update request: ' + e.message);
  69. });
  70.  
  71. req.write(queryString);
  72.  
  73. req.end();
  74. }
  75.  
  76. exports.getAccessToken = getAccessToken;
  77. exports.search = search;
Add Comment
Please, Sign In to add comment