Skip to content

Instantly share code, notes, and snippets.

@nagyv
Created Apr 19, 2019
Embed
What would you like to do?
get and create record in GameSparks Data Service
const axios = require('axios')
require('dotenv').config()
const username = process.env.LOGIN
const password = process.env.PASSWORD
const game = process.env.GAME
async function jwtAuth(username, password, game, filter) {
const response = await axios({
url: `https://auth.gamesparks.net/restv2/auth/game/${game}/jwt`,
auth: {
username, password
}
})
return response.data['X-GS-JWT']
}
async function getEndpoints(token, game) {
const response = await axios({
url: `https://config2.gamesparks.net/restv2/game/${game}/endpoints`,
headers: {
"X-GS-JWT": token
}
})
return response.data
}
async function getToken(base, token, game, id) {
const response = await axios({
url: `${base}/restv2/game/${game}/data/RegistrationTokens/${id}`,
headers: {
"X-GS-JWT": token,
"Content-Type": "application/json"
}
})
return response.data
}
async function createToken(base, token, game) {
const item = {
"owner": null,
"gameType": "GAME_BASIC",
"created": Date.now(),
"updated": Date.now(),
"token": "teszt"
}
try {
const response = await axios({
url: `${base}/restv2/game/${game}/data/RegistrationTokens/${item.token}`,
method: 'put',
data: item,
headers: {
"X-GS-JWT": token,
"Content-Type": "application/json"
}
})
return response.data
} catch (err) {
return [err.response.status, err.response.statusText, err.response]
}
}
async function run() {
const token = await jwtAuth(username, password, game, 'data')
const urls = await getEndpoints(token, game)
const base = urls.liveNosql
const getResponse = await getToken(base, token, game, 'bika')
console.log('Token received', getResponse)
const createResponse = await createToken(base, token, game)
console.log('Created', createResponse)
}
run().then(() => {
console.log('Exit')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment