app.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. let { appKey, appSecret, consumerKey } = require('./credentials.json');
  2. const fs = require('fs');
  3. const ovh = require('ovh')({
  4. endpoint: 'ovh-eu',
  5. appKey,
  6. appSecret,
  7. consumerKey
  8. });
  9. function requestConsumerKey() {
  10. return ovh.requestPromised('POST', '/auth/credential', {
  11. 'accessRules': [
  12. { 'method': 'GET', 'path': '/me'},
  13. { 'method': 'GET', 'path': '/domain/zone'},
  14. // { 'method': 'POST', 'path': '/domain/*'},
  15. // { 'method': 'PUT', 'path': '/domain/*'},
  16. // { 'method': 'DELETE', 'path': '/domain/*'}
  17. ]
  18. })
  19. .then(function (credentials) {
  20. console.log('requestConsumerKey', credentials);
  21. consumerKey = credentials.consumerKey;
  22. fs.writeFileSync('credentials.json', JSON.stringify({
  23. appKey, appSecret, consumerKey
  24. }));
  25. })
  26. .catch(function (err) {
  27. console.log('requestConsumerKey err', err);
  28. //Return an error object like this {error: statusCode, message: message}
  29. });
  30. }
  31. function readDomains() {
  32. console.log('readDomains');
  33. return ovh.requestPromised('GET', '/domain/zone')
  34. .then(response => {
  35. console.log(response)
  36. })
  37. .catch(err => {
  38. console.log(err);
  39. })
  40. }
  41. function readMe() {
  42. console.log('readMe');
  43. return ovh.requestPromised('GET', '/me')
  44. .then(response => {
  45. console.log(response)
  46. })
  47. .catch(err => {
  48. console.log(err);
  49. })
  50. }
  51. if(consumerKey) {
  52. readMe();
  53. readDomains();
  54. // readCurrentConsumerKey();
  55. }
  56. else {
  57. requestConsumerKey();
  58. }
  59. function readCurrentConsumerKey(){
  60. console.log('readCurrentConsumerKey');
  61. return ovh.requestPromised('GET', '/auth/currentCredential')
  62. .then(response => {
  63. console.log(response)
  64. })
  65. .catch(err => {
  66. console.log(err);
  67. })
  68. }