LwFacebookLambdaFunction.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. var http = require("https");
  9. var facebookClientID = "{{facebookClientID}}";
  10. var facebookClientSecret = "{{facebookClientSecret}}";
  11. var facebookRedirectUrl = "{{facebookRedirectUrl}}";
  12. var getFacebookRefreshToken = function(code, callback)
  13. {
  14. var options = {
  15. hostname: 'graph.facebook.com',
  16. path: '/v2.3/oauth/access_token',
  17. port: 443,
  18. method : "POST",
  19. headers : {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
  20. };
  21. var body = "client_id=" + facebookClientID + "&";
  22. body += "redirect_uri=" + facebookRedirectUrl + "&";
  23. body += "client_secret=" + facebookClientSecret + "&";
  24. body += "code=" + code;
  25. console.log(body);
  26. var req = http.request(options, function(res) {
  27. res.setEncoding('utf8');
  28. var store = "";
  29. res.on('data', function (chunk) {
  30. console.log(chunk);
  31. store += chunk;
  32. });
  33. res.on('end', function() {
  34. var response = JSON.parse(store);
  35. var data = {
  36. access_token : response.access_token,
  37. expires_in : response.expires_in,
  38. };
  39. console.log(data);
  40. callback(null, data);
  41. });
  42. req.on('error', function(e) {
  43. console.log('problem with request: ' + e.message);
  44. callback(e);
  45. });
  46. });
  47. req.write(body);
  48. req.end();
  49. };
  50. var refreshAccessToken = function(refreshToken, callback)
  51. {
  52. var options = {
  53. hostname: 'graph.facebook.com',
  54. path: '/v2.3/oauth/access_token',
  55. port: 443,
  56. method : "POST",
  57. headers : {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
  58. };
  59. var body = "grant_type=fb_exchange_token&";
  60. body += "fb_exchange_token=" + refreshToken + "&";
  61. body += "redirect_uri=" + facebookRedirectUrl + "&";
  62. body += "client_id=" + facebookClientID + "&";
  63. body += "client_secret=" + facebookClientSecret;
  64. console.log(body);
  65. var req = http.request(options, function(res) {
  66. res.setEncoding('utf8');
  67. var store = "";
  68. res.on('data', function (chunk) {
  69. console.log(chunk);
  70. store += chunk;
  71. });
  72. res.on('end', function() {
  73. var response = JSON.parse(store);
  74. var data = {
  75. access_token : response.access_token,
  76. expires_in : response.expires_in,
  77. refresh_token : response.refresh_token,
  78. };
  79. console.log(data);
  80. callback(null, data);
  81. });
  82. req.on('error', function(e) {
  83. console.log('problem with request: ' + e.message);
  84. callback(e);
  85. });
  86. });
  87. req.write(body);
  88. req.end();
  89. };
  90. exports.{{handlerName}} = function(event, context) {
  91. if(typeof event.code != 'undefined') {
  92. var code = event.code;
  93. console.log(code);
  94. getFacebookRefreshToken(code, function(err, data)
  95. {
  96. if(err)
  97. {
  98. context.fail(err);
  99. }
  100. else
  101. {
  102. context.succeed (data);
  103. }
  104. });
  105. }
  106. else if(typeof event.refresh_token != 'undefined') {
  107. var refresh_token = event.refresh_token;
  108. console.log(refresh_token);
  109. refreshAccessToken(refresh_token, function(err, data)
  110. {
  111. if(err)
  112. {
  113. context.fail(err);
  114. }
  115. else
  116. {
  117. context.succeed (data);
  118. }
  119. });
  120. }
  121. };