-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain_auth.js
70 lines (51 loc) · 2.03 KB
/
domain_auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Authenticating Requests with JS - convert to Ruby
// Get Access token using client credential
// https://developer.domain.com.au/docs/authorisation/client-credentials-grant
// For HTTP requests use axios - equilivent to Ruby HTTParty
const axios = require('axios')
const querystring = require('querystring')
/**
* Get access token using client credential auth flow
* @param {string} clientId Your client's Id
* @param {string} secret Your client's secret
*/
function getAccessToken(clientId, secret) {
//querysting.stringify method produces a URL string from the object. Basically translates into grant_type=client_credentials&scope=api_agencies_read&scope=api_listings_read
var data = querystring.stringify({
grant_type: 'client_credentials',
scope: 'api_agencies_read api_listings_read'
});
// Now make http request
return axios.post('https://auth.domain.com.au/v1/connect/token', data, {
headers: {
'Authorization': `Basic ${base64(`${clientId}:${secret}`)}`,
'Content-Type': 'application/x-www-form-urlencoded',
}
}).then(result => { // .then returns a promise (result)
const { access_token } = result.data;
//store your access_token for authenticating your API calls
console.log(access_token);
}).catch(err => console.error(err.response.data)) //.catch deals with rejected cases
}
function base64(str) { // node method
return Buffer.from(str).toString('base64')
}
module.exports = getAccessToken
// Making a request to get listing by id
const axios = require('axios')
/**
* Get listing by id
* @param {string} listingId
* @param {string} accessToken
*/
function getListingById(listingId, accessToken) {
return axios.get(`https://api.domain.com.au/v1/listings/${listingId}`, {
headers: {
'Authorization': `Bearer ${accessToken}`,
}
}).then(result => {
const { data } = result;
console.log(data);
}).catch(err => console.error(err.response.data))
}
module.exports = getListingById