์๋น์ค ๋ฐ ์ก์ธ์ค ํ ํฐ ์ง์
Use Access Tokens and Service Tokens to authenticate requests in Cloud Code modules.
์ฝ๋ ์๊ฐ 3๋ถ์ต๊ทผ ์
๋ฐ์ดํธ: 7๋ฌ ์
๋ชจ๋์์ ์ธํฐํ์ด์ค๋ ๊ณผ ์ ์ ๊ณตํฉ๋๋ค.
IExecutionContextAccessTokenServiceTokenํ ํฐ ์ ํ | ์ถ์ฒ | ๋ฐ์ดํฐ ์ก์ธ์ค | ์ฌ์ฉ |
|---|---|---|---|
| Authentication ์๋น์ค ์์ ์์ฑ | ์ธ์ฆ๋ ํ๋ ์ด์ด๋ง | |
| Cloud Code์์ ์์ฑ | ํฌ๋ก์ค ํ๋ ์ด์ด ๋ฐ์ดํฐ ์ก์ธ์ค | |
๋ค๋ฅธ UGS ์๋น์ค๋ฅผ ํธ์ถํ ๋ ์ด๋ฌํ ํ ํฐ์ ์ฌ์ฉํ์ฌ ํ๋ ์ด์ด๋ Cloud Code๋ก ์ธ์ฆํ ์ ์์ต๋๋ค.
์์ธํ ๋ด์ฉ์ ์ธ์ฆ ํ์ด์ง๋ฅผ ์ฐธ๊ณ ํ์ญ์์ค.
๊ฐ์
ํ๋ ์ด์ด๋ก ์ธ์ฆํ ์ง ์ ๋ขฐํ ์ ์๋ ํด๋ผ์ด์ธํธ๋ก ์ธ์ฆํ ์ง ํ๊ฐํฉ๋๋ค.
๋ชจ๋ ์ธํฐํ์ด์ค๋ ๊ณผ ์ ์ ๊ณตํฉ๋๋ค.
IExecutionContextAccessTokenServiceToken๋ชจ๋์ ๋ชฉ์ ์ด ์ธ์ฆ๋ ํ๋ ์ด์ด์ ๋ฐ์ดํฐ์๋ง ์ก์ธ์คํ๋ ๊ฒ์ด๋ผ๋ฉด, ์ ์ฌ์ฉํ์ฌ ๋ชจ๋ ์๋ํฌ์ธํธ๋ฅผ ํธ์ถํ๋ ํ๋ ์ด์ด๋ก ์ธ์ฆํด์ผ ํฉ๋๋ค. ๊ทธ๋ฌ๋ฉด ํ๋ ์ด์ด๊ฐ ์์ ์ ๋ฐ์ดํฐ์๋ง ์ก์ธ์คํ๊ณ ์ํธ ์์ฉํ ์ ์์ต๋๋ค.
AccessToken๋ชจ๋์ ๋ชฉ์ ์ด ํฌ๋ก์ค ํ๋ ์ด์ด ๋ฐ์ดํฐ์ ์ก์ธ์คํ๋ ๊ฒ์ด๋ผ๋ฉด, ์ ์ฌ์ฉํ์ฌ Cloud Code๋ก ์ธ์ฆํด์ผ ํฉ๋๋ค. ๊ทธ๋ฌ๋ฉด ๋ชจ๋์ด ํฌ๋ก์ค ํ๋ ์ด์ด ๋ฐ์ดํฐ์ ์ก์ธ์คํ๊ณ ์ํธ ์์ฉํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ฉด ์
์์ ์ธ ํ๋ ์ด์ด๊ฐ ๋ชจ๋ ์๋ํฌ์ธํธ๋ฅผ ํธ์ถํ์ฌ ์๋ํ์ง ์์ ๋ถ๋ถ์์ ํฌ๋ก์ค ํ๋ ์ด์ด ๋ฐ์ดํฐ์ ์ก์ธ์คํ๊ฒ ๋ ์ ์์ต๋๋ค. ์ด๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ๋ชจ๋์ ์ก์ธ์ค ์ ์ด ๊ท์น์ผ๋ก ๋ณดํธํด์ผ ํฉ๋๋ค.
ServiceTokenํฌ๋ก์ค ํ๋ ์ด์ด ๋ฐ์ดํฐ์ ์ํธ ์์ฉํ๋ ๋ฐฉ๋ฒ์ ์์ธํ ์์๋ณด๋ ค๋ฉด ํ๋ ์ด์ด ๊ฐ ๋ฐ์ดํฐ ๊ธฐ์ ์๋ฃ๋ฅผ ์ฐธ๊ณ ํ์ญ์์ค.
์ก์ธ์ค ํ ํฐ ์ง์
์ก์ธ์ค ํ ํฐ์ Authentication ์๋น์ค ์์ฑํ๋ JWT์
๋๋ค. ์ก์ธ์ค ํ ํฐ์ ์ฌ์ฉํ์ฌ ํ๋ ์ด์ด๋ฅผ ์ธ์ฆํ ์ ์์ต๋๋ค.
์ธ์ฆ๋ ํ๋ ์ด์ด๊ฐ Cloud Code ๋ชจ๋์ ํธ์ถํ๋ฉด, ํ๋ ์ด์ด์ ์ก์ธ์ค ํ ํฐ์ ์ธํฐํ์ด์ค์ ํ๋กํผํฐ๋ก ๋ชจ๋์ ์ ๋ฌํฉ๋๋ค.
IExecutionContextAccessTokenCloud Code C# Game SDK๋ก ์ก์ธ์ค ํ ํฐ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ค๋ฅธ UGS API์์ ์ก์ธ์ค ํ ํฐ์ ์ฌ์ฉํ๋ ค๋ฉด, ์ธ์ฆ ํค๋์์ ํ ํฐ์ bearer ํ ํฐ์ผ๋ก ์ ๋ฌํด์ผ ํฉ๋๋ค.
์ง์๋๋ ์๋น์ค
์ก์ธ์ค ํ ํฐ์ ์ง์ํ๋ ์๋น์ค๋ฅผ ํ์ธํ๋ ค๋ฉด ์๋ ํ๋ฅผ ์ฐธ๊ณ ํ์ญ์์ค.
์ ๊ณต์ | ์ง์๋๋ ์๋น์ค |
|---|---|
| Cloud Code C# Client SDK | ๋ชจ๋ Cloud Code C# Client SDK๋ ํด๋น ์๋น์ค API์ ๋์ผํ ์ก์ธ์ค ํ ํฐ์ ์ง์ํฉ๋๋ค. |
| UGS ํด๋ผ์ด์ธํธ API | Authentication ์๋น์ค๋ฅผ ์ง์ํ๋ ๋ชจ๋ UGS ์๋น์ค๋ฅผ ์ง์ํฉ๋๋ค. |
| Cloud Code C# Admin SDK | ์ง์๋์ง ์์ต๋๋ค. ๋์ ์๋น์ค ๊ณ์ ์ธ์ฆ์ ์ฌ์ฉํ์ญ์์ค. |
| UGS ๊ด๋ฆฌ์ API | ์ง์๋์ง ์์ต๋๋ค. ๋์ ์๋น์ค ๊ณ์ ์ธ์ฆ์ ์ฌ์ฉํ์ญ์์ค. |
Cloud Code C# Client SDK๋ก ํ ํฐ ์ฌ์ฉ
Cloud Code C# Client SDK๋ก ์ ์ฌ์ฉํ๋ ค๋ฉด ์ธํฐํ์ด์ค์์ ํ ํฐ์ ์ ๋ฌํด์ผ ํฉ๋๋ค.
AccessTokenIExecutionContext์๋๋ ๋ชจ๋์ ํธ์ถํ๋ ํ๋ ์ด์ด์ ์ฌํ ์์ก์ ๋๋ฆฌ๋ ์์์
๋๋ค.
[CloudCodeFunction("IncrementBalance")]public async Task<ApiResponse<CurrencyBalanceResponse>> IncrementPlayerBalance(IGameApiClient gameApiClient, IExecutionContext ctx, string currencyId){ ... try { // Increment the balance of the currency for the player calling the module var res = await gameApiClient.EconomyCurrencies.IncrementPlayerCurrencyBalanceAsync(ctx, ctx.AccessToken, ctx.ProjectId, ctx.PlayerId, currencyId, new CurrencyModifyBalanceRequest(currencyId, 10)); _logger.LogInformation("Incremented currency {currencyId} balance by {amount}", currencyId, 10); return res; } catch (ApiException e) { _logger.LogError("Failed to increment {currencyId} balance for the player. Error: {error}", currencyId, e.Message); throw new Exception($"Failed to increment {currencyId} balance for playerId {ctx.PlayerId}. Error: {e.Message}"); }}
UGS ํด๋ผ์ด์ธํธ API๋ก ์ฌ์ฉ
UGS ํด๋ผ์ด์ธํธ API๋ก ์ ์ฌ์ฉํ๋ ค๋ฉด ์ธ์ฆ ํค๋์ ํ ํฐ์ bearer ํ ํฐ์ผ๋ก ์ ๋ฌํด์ผ ํฉ๋๋ค.
AccessTokenpublic class Relationships{ private readonly RestClient _httpClient; public Relationships() { _httpClient = new RestClient("https://social.services.api.unity.com/v1/relationships"); } public async Task<List<RelationshipsApiResponse?>> GetRelationships(IExecutionContext context) { var request = new RestRequest() { // Pass the access token as a bearer token in the header Authenticator = new JwtAuthenticator(context.AccessToken) }; .... }}
์๋น์ค ํ ํฐ ์ง์
์๋น์ค ํ ํฐ์ Cloud Code๊ฐ ์์ฑํ๋ JWT์
๋๋ค. ์๋น์ค ํ ํฐ์ ์ฌ์ฉํ์ฌ Cloud Code๋ก ์๋น์ค๋ฅผ ์ธ์ฆํ ์ ์์ต๋๋ค. ์ด ํ ํฐ์ ํ ํฐ ๊ตํ ํ๋ก์ธ์ค๋ฅผ ๊ฑฐ์น ์๋น์ค ๊ณ์ ํ ํฐ์
๋๋ค.
Cloud Code C# Client SDK๋ก ์๋น์ค ํ ํฐ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ค๋ฅธ API๋ก ์๋น์ค ํ ํฐ์ ์ฌ์ฉํ๋ ค๋ฉด ์ธ์ฆ ํค๋์์ ํ ํฐ์ bearer ํ ํฐ์ผ๋ก ์ ๋ฌํด์ผ ํฉ๋๋ค.
์ง์๋๋ ์๋น์ค
์๋น์ค ํ ํฐ์ ์ง์ํ๋ ์๋น์ค ๋ชฉ๋ก์ ์๋ ํ๋ฅผ ์ฐธ๊ณ ํ์ญ์์ค.
์ ๊ณต์ | ์ง์๋๋ ์๋น์ค |
|---|---|
| Cloud Code C# Client SDK | ์ฌ์ฉํ ์ ์๋ SDK ๋ชฉ๋ก์ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฐธ๊ณ ํ์ญ์์ค. |
| UGS ํด๋ผ์ด์ธํธ API | |
| Cloud Code C# Admin SDK | ์ง์๋์ง ์์ต๋๋ค. ๋์ ์๋น์ค ๊ณ์ ์ธ์ฆ์ ์ฌ์ฉํ์ญ์์ค. |
| UGS ๊ด๋ฆฌ์ API | ์ง์๋์ง ์์ต๋๋ค. ๋์ ์๋น์ค ๊ณ์ ์ธ์ฆ์ ์ฌ์ฉํ์ญ์์ค. |
Cloud Code C# Client SDK๋ก ํ ํฐ ์ฌ์ฉ
Cloud Code C# Client SDK๋ก ์ ์ฌ์ฉํ๋ ค๋ฉด ์ธํฐํ์ด์ค์์ ํ ํฐ์ ์ ๋ฌํด์ผ ํฉ๋๋ค.
ServiceTokenIExecutionContext์๋๋ ํ๋ ์ด์ด ID๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ํ๋ ์ด์ด์ ์ฌํ ์์ก์ ๋๋ฆฌ๋ ์์์
๋๋ค.
[CloudCodeFunction("IncrementBalance")]public async Task<ApiResponse<CurrencyBalanceResponse>> IncrementPlayerBalance(IGameApiClient gameApiClient, IExecutionContext ctx, string currencyId, string playerId){ ... try { // Increment the balance of the currency for the player ID passed in, authenticated as Cloud Code var res = await gameApiClient.EconomyCurrencies.IncrementPlayerCurrencyBalanceAsync(ctx, ctx.ServiceToken, ctx.ProjectId, playerId, currencyId, new CurrencyModifyBalanceRequest(currencyId, 10)); _logger.LogInformation("Incremented currency {currencyId} balance by {amount}", currencyId, 10); return res; } catch (ApiException e) { _logger.LogError("Failed to increment {currencyId} balance for the player. Error: {error}", currencyId, e.Message); throw new Exception($"Failed to increment {currencyId} balance for playerId {ctx.PlayerId}. Error: {e.Message}"); }}
UGS ํด๋ผ์ด์ธํธ API๋ก ํ ํฐ ์ฌ์ฉ
UGS ํด๋ผ์ด์ธํธ API๋ก ์ ์ฌ์ฉํ๋ ค๋ฉด ์ธ์ฆ ํค๋์ ํ ํฐ์ bearer ํ ํฐ์ผ๋ก ์ ๋ฌํด์ผ ํฉ๋๋ค.
ServiceToken์๋ฅผ ๋ค์ด Cloud Save API์ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ๋ฉด ์ ์ฌ์ฉํ์ฌ ์ธ์ฆํ๊ณ ํฌ๋ก์ค ํ๋ ์ด์ด ๋ฐ์ดํฐ์ ์ํธ ์์ฉํ ์ ์์ต๋๋ค. ์๋๋ ํ๋ ์ด์ด ID๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ํ๋ ์ด์ด์ ๋ํด ์ ์ฅ๋ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๋ ์์์
๋๋ค.
ServiceTokenusing System.Net;using Unity.Services.CloudCode.Core;using Newtonsoft.Json;using RestSharp;using RestSharp.Authenticators;namespace ExampleModule;public interface ICloudSaveData{ public Task<CloudSaveResponse?> GetData(IExecutionContext context, string playerId);}public class CloudSaveResponse{ public CloudSaveItem[] Results { get; set; }}public class CloudSaveItem{ public string Key { get; set; } public object Value { get; set; }}public class CloudSaveData : ICloudSaveData{ private readonly RestClient _httpClient; public async Task<CloudSaveResponse?> GetData(IExecutionContext context, string playerId) { var request = new RestRequest($"v1/data/projects/{context.ProjectId}/players/{playerId}/items") { // Authenticate as Cloud Code to interact with cross-player data Authenticator = new JwtAuthenticator(context.ServiceToken) }; // Pass through the analytics user id and Unity installation id to the service. if (context.AnalyticsUserId != null) request = request.AddHeader("analytics-user-id", context.AnalyticsUserId); if (context.UnityInstallationId != null) request = request.AddHeader("unity-installation-id", context.UnityInstallationId); RestResponse response = await _httpClient.ExecuteGetAsync(request); if (response.Content == null) throw new Exception("Failed to load data from Cloud Save: Content was null"); if (response.StatusCode != HttpStatusCode.OK) throw new Exception("Failed to load data from Cloud Save: " + response.Content); return JsonConvert.DeserializeObject<CloudSaveResponse>(response.Content); }}