Sfoglia il codice sorgente

sample app that can: read me, read my domains

Benoît Hubert 8 anni fa
parent
commit
8d1ca6ad45
3 ha cambiato i file con 81 aggiunte e 2 eliminazioni
  1. 72 0
      app.js
  2. 3 2
      credentials.sample.json
  3. 6 0
      package.json

+ 72 - 0
app.js

@@ -0,0 +1,72 @@
+let { appKey, appSecret, consumerKey } = require('./credentials.json');
+const fs = require('fs');
+const ovh = require('ovh')({
+  endpoint: 'ovh-eu',
+  appKey,
+  appSecret,
+  consumerKey
+});
+
+function requestConsumerKey() {
+  return ovh.requestPromised('POST', '/auth/credential', {
+    'accessRules': [
+      { 'method': 'GET', 'path': '/me'},
+      { 'method': 'GET', 'path': '/domain/zone'},
+      // { 'method': 'POST', 'path': '/domain/*'},
+      // { 'method': 'PUT', 'path': '/domain/*'},
+      // { 'method': 'DELETE', 'path': '/domain/*'}
+    ]
+  })
+  .then(function (credentials) {
+    console.log('requestConsumerKey', credentials);
+    consumerKey = credentials.consumerKey;
+    fs.writeFileSync('credentials.json', JSON.stringify({
+      appKey, appSecret, consumerKey
+    }));
+  })
+  .catch(function (err) {
+    console.log('requestConsumerKey err', err);
+    //Return an error object like this {error: statusCode, message: message}
+  });
+}
+
+function readDomains() {
+  console.log('readDomains');
+  return ovh.requestPromised('GET', '/domain/zone')
+  .then(response => {
+    console.log(response)
+  })
+  .catch(err => {
+    console.log(err);
+  })
+}
+function readMe() {
+  console.log('readMe');
+  return ovh.requestPromised('GET', '/me')
+  .then(response => {
+    console.log(response)
+  })
+  .catch(err => {
+    console.log(err);
+  })
+}
+
+if(consumerKey) {
+  readMe();
+  readDomains();
+  // readCurrentConsumerKey();
+}
+else {
+  requestConsumerKey();
+}
+
+function readCurrentConsumerKey(){
+  console.log('readCurrentConsumerKey');
+  return ovh.requestPromised('GET', '/auth/currentCredential')
+  .then(response => {
+    console.log(response)
+  })
+  .catch(err => {
+    console.log(err);
+  })
+}

+ 3 - 2
credentials.sample.json

@@ -1,4 +1,5 @@
 {
-  "key": "your-api-key",
-  "secret": "your-api-secret"
+  "appKey": "your-api-key",
+  "appSecret": "your-api-secret",
+  "consumerKey": "api-consumer-key"
 }

+ 6 - 0
package.json

@@ -0,0 +1,6 @@
+{
+  "name": "ovh-dnz-ozne",
+  "dependencies": {
+    "ovh": "^2.0.1"
+  }
+}