LwALambdaFunction.js 3.7 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 amznClientID = "{{amznClientID}}";
  10. var amznClientSecret = "{{amznClientSecret}}";
  11. var amznRedirectUrl = "{{amznRedirectUrl}}";
  12. var getAmazonRefreshToken = function(code, callback)
  13. {
  14. var options = {
  15. hostname: 'api.amazon.com',
  16. path: '/auth/o2/token',
  17. port: 443,
  18. method : "POST",
  19. headers : {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
  20. };
  21. var body = "grant_type=authorization_code&";
  22. body += "code=" + code + "&";
  23. body += "client_id=" + amznClientID + "&";
  24. body += "client_secret=" + amznClientSecret + "&";
  25. body += "redirect_uri=" + amznRedirectUrl;
  26. console.log(body);
  27. var req = http.request(options, function(res) {
  28. res.setEncoding('utf8');
  29. var store = "";
  30. res.on('data', function (chunk) {
  31. console.log(chunk);
  32. store += chunk;
  33. });
  34. res.on('end', function() {
  35. var response = JSON.parse(store);
  36. var data = {
  37. access_token : response.access_token,
  38. expires_in : response.expires_in,
  39. refresh_token : response.refresh_token,
  40. };
  41. console.log(data);
  42. callback(null, data);
  43. });
  44. req.on('error', function(e) {
  45. console.log('problem with request: ' + e.message);
  46. callback(e);
  47. });
  48. });
  49. req.write(body);
  50. req.end();
  51. };
  52. var refreshAccessToken = function(refreshToken, callback)
  53. {
  54. var options = {
  55. hostname: 'api.amazon.com',
  56. path: '/auth/o2/token',
  57. port: 443,
  58. method : "POST",
  59. headers : {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
  60. };
  61. var body = "grant_type=refresh_token&";
  62. body += "refresh_token=" + refreshToken + "&";
  63. body += "client_id=" + amznClientID + "&";
  64. body += "client_secret=" + amznClientSecret;
  65. console.log(body);
  66. var req = http.request(options, function(res) {
  67. res.setEncoding('utf8');
  68. var store = "";
  69. res.on('data', function (chunk) {
  70. console.log(chunk);
  71. store += chunk;
  72. });
  73. res.on('end', function() {
  74. var response = JSON.parse(store);
  75. var data = {
  76. access_token : response.access_token,
  77. expires_in : response.expires_in,
  78. refresh_token : response.refresh_token,
  79. };
  80. console.log(data);
  81. callback(null, data);
  82. });
  83. req.on('error', function(e) {
  84. console.log('problem with request: ' + e.message);
  85. callback(e);
  86. });
  87. });
  88. req.write(body);
  89. req.end();
  90. };
  91. exports.{{handlerName}} = function(event, context) {
  92. if(typeof event.code != 'undefined') {
  93. var code = event.code;
  94. console.log(code);
  95. getAmazonRefreshToken(code, function(err, data)
  96. {
  97. if(err)
  98. {
  99. context.fail(err);
  100. }
  101. else
  102. {
  103. context.succeed (data);
  104. }
  105. });
  106. }
  107. else if(typeof event.refresh_token != 'undefined') {
  108. var refresh_token = event.refresh_token;
  109. console.log(refresh_token);
  110. refreshAccessToken(refresh_token, function(err, data)
  111. {
  112. if(err)
  113. {
  114. context.fail(err);
  115. }
  116. else
  117. {
  118. context.succeed (data);
  119. }
  120. });
  121. }
  122. };