Hey
I have a cards game where players gets awarded an amount of currency after each game depending on whether they won lost or tied.
So, I’m trying to use CloudCode to do this since the Economy SDK seems to have no way to award multiple players with currency.
So, I am trying to authenticate to my server like this:
await UnityServices.InitializeAsync();
await ServerAuthenticationService.Instance.SignInWithServiceAccountAsync(keyId, keyPass);
But before I can call CloudCode, I am verifying some information in CloudSave, and I get this error
CloudSaveException: Access token is missing - ensure you are signed in through the Authentication SDK and try again.
If I perform an anonymous authentication before calling cloud save, it continues excecution, but then when calling CloudCode I get this error:
Error: Request failed with status code 403\m (…) you are not permitted to access this user’s data
Any idea on what’s the problem and how can I fix it? BTW I don’t wanna call CloudCode nor CloudSave using AuthenticationService.Instance authentications, I want to call it as a server so it can have access to modifying multiple players currencies. This is because, if I call CloudCode from my server after using AuthenticationService.Instance .SingInAnonymously(), I get this error form cloud code when trying to modify the currencies:
Error: Request failed with status code 403\m (…) you are not permitted to access this user’s data
This is my CloudCodeAPI function:
const { CurrenciesApi } = require("@unity-services/economy-2.4");
const { DataApi } = require("@unity-services/cloud-save-1.2");
const badRequestError = 400;
const tooManyRequestsError = 429;
module.exports = async ({ params, context, logger }) => {
try{
const { projectId, playerId, accessToken } = context;
const currenciesApi = new CurrenciesApi({ accessToken });
const cloudSaveApi = new DataApi({ accessToken });
const playersAndPointsList = params.playersAndPointsList;
logger.debug("awardPointsToPlayers received params: "+JSON.stringify(playersAndPointsList));
for(let i = 0; i < playersAndPointsList.length; i++){
var playerAndPoints = playersAndPointsList[i];
await currenciesApi.incrementPlayerCurrencyBalance({
currencyId: "POINTS",
playerId: playerAndPoints.playerId,
projectId: context.projectId,
currencyModifyBalanceRequest: { amount: playerAndPoints.pointsToAward },
});
}
const returnObject = redeemResult.data;
// Let Cloud Save know that the Starter Pack has been claimed by this player.
await cloudSaveApi.setItem(projectId, playerId, { key: "starterPackStatus", value: { claimed: true } });
return returnObject;
}
catch (error) {
logger.error(error);
}
}