void callApiAsync(String targentStep) { if (AppStoreOnboardApi.tokenInfo.access_token == null) { UnityOAuth.GetAuthorizationCodeAsync(AppStoreOnboardApi.oauthClientId, (response) => { if (response.AuthCode != null) { string authcode = response.AuthCode; UnityWebRequest request = AppStoreOnboardApi.GetAccessToken(authcode); TokenInfo tokenInfoResp = new TokenInfo(); ReqStruct reqStruct = new ReqStruct(); reqStruct.request = request; reqStruct.resp = tokenInfoResp; reqStruct.targetStep = targentStep; requestQueue.Enqueue(reqStruct); } else { Debug.Log("Failed: " + response.Exception.ToString()); isOperationRunning = false; } }); } else { UnityWebRequest request = AppStoreOnboardApi.GetUserId(); UserIdResponse userIdResp = new UserIdResponse(); ReqStruct reqStruct = new ReqStruct(); reqStruct.request = request; reqStruct.resp = userIdResp; reqStruct.targetStep = targentStep; requestQueue.Enqueue(reqStruct); } }
void CheckRequestUpdate() { if (requestQueue.Count <= 0) { return; } ReqStruct reqStruct = requestQueue.Dequeue(); UnityWebRequest request = reqStruct.request; GeneralResponse resp = reqStruct.resp; if (request != null && request.isDone) { if (request.error != null) { Debug.LogError(request.error); isOperationRunning = false; } else { if (request.downloadHandler.text.Contains(AppStoreOnboardApi.tokenExpiredInfo)) { UnityWebRequest newRequest = AppStoreOnboardApi.RefreshToken(); TokenInfo tokenInfoResp = new TokenInfo(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = tokenInfoResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else { if (resp.GetType() == typeof(TokenInfo)) { resp = JsonUtility.FromJson <TokenInfo> (request.downloadHandler.text); AppStoreOnboardApi.tokenInfo.access_token = ((TokenInfo)resp).access_token; if (AppStoreOnboardApi.tokenInfo.refresh_token == null || AppStoreOnboardApi.tokenInfo.refresh_token == "") { AppStoreOnboardApi.tokenInfo.refresh_token = ((TokenInfo)resp).refresh_token; } UnityWebRequest newRequest = AppStoreOnboardApi.GetUserId(); UserIdResponse userIdResp = new UserIdResponse(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = userIdResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else if (resp.GetType() == typeof(UserIdResponse)) { resp = JsonUtility.FromJson <UserIdResponse> (request.downloadHandler.text); AppStoreOnboardApi.userId = ((UserIdResponse)resp).sub; UnityWebRequest newRequest = AppStoreOnboardApi.GetOrgId(Application.cloudProjectId); OrgIdResponse orgIdResp = new OrgIdResponse(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = orgIdResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else if (resp.GetType() == typeof(OrgIdResponse)) { resp = JsonUtility.FromJson <OrgIdResponse> (request.downloadHandler.text); AppStoreOnboardApi.orgId = ((OrgIdResponse)resp).org_foreign_key; UnityWebRequest newRequest = AppStoreOnboardApi.GetOrgRoles(); OrgRoleResponse orgRoleResp = new OrgRoleResponse(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = orgRoleResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else if (resp.GetType() == typeof(OrgRoleResponse)) { resp = JsonUtility.FromJson <OrgRoleResponse> (request.downloadHandler.text); List <string> roles = ((OrgRoleResponse)resp).roles; if (roles.Contains("owner")) { ownerAuthed = true; if (reqStruct.targetStep == STEP_GET_CLIENT) { UnityWebRequest newRequest = AppStoreOnboardApi.GetUnityClientInfo(Application.cloudProjectId); UnityClientResponseWrapper clientRespWrapper = new UnityClientResponseWrapper(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = clientRespWrapper; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else if (reqStruct.targetStep == STEP_UPDATE_CLIENT) { UnityClientInfo unityClientInfo = new UnityClientInfo(); unityClientInfo.ClientId = unityClientID.stringValue; string callbackUrl = callbackUrl_in_memory; // read xiaomi from user input XiaomiSettings xiaomi = new XiaomiSettings(); xiaomi.appId = xiaomiAppID.stringValue; xiaomi.appKey = xiaomiAppKey.stringValue; xiaomi.appSecret = appSecret_in_memory; UnityWebRequest newRequest = AppStoreOnboardApi.UpdateUnityClient(Application.cloudProjectId, unityClientInfo, xiaomi, callbackUrl); UnityClientResponse clientResp = new UnityClientResponse(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = clientResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else if (reqStruct.targetStep == STEP_UPDATE_CLIENT_SECRET) { string clientId = unityClientID.stringValue; UnityWebRequest newRequest = AppStoreOnboardApi.UpdateUnityClientSecret(clientId); UnityClientResponse clientResp = new UnityClientResponse(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = clientResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } } else if (roles.Contains("user") || roles.Contains("manager")) { ownerAuthed = false; if (reqStruct.targetStep == STEP_GET_CLIENT) { UnityWebRequest newRequest = AppStoreOnboardApi.GetUnityClientInfo(Application.cloudProjectId); UnityClientResponseWrapper clientRespWrapper = new UnityClientResponseWrapper(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = clientRespWrapper; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else { Debug.LogError("Permision denied."); isOperationRunning = false; } } else { Debug.LogError("Permision denied."); isOperationRunning = false; } } else if (resp.GetType() == typeof(UnityClientResponseWrapper)) { string raw = "{ \"array\": " + request.downloadHandler.text + "}"; resp = JsonUtility.FromJson <UnityClientResponseWrapper> (raw); // only one element in the list if (((UnityClientResponseWrapper)resp).array.Length > 0) { UnityClientResponse unityClientResp = ((UnityClientResponseWrapper)resp).array [0]; unityClientID.stringValue = unityClientResp.client_id; unityClientKey.stringValue = unityClientResp.client_secret; unityClientRSAPublicKey.stringValue = unityClientResp.channel.publicRSAKey; clientSecret_in_memory = unityClientResp.channel.channelSecret; callbackUrl_in_memory = unityClientResp.channel.callbackUrl; callbackUrl_last = callbackUrl_in_memory; foreach (ThirdPartySettingsResponse thirdPartySetting in unityClientResp.channel.thirdPartySettings) { if (thirdPartySetting.appType.Equals(AppStoreOnboardApi.xiaomiAppType, StringComparison.InvariantCultureIgnoreCase)) { xiaomiAppID.stringValue = thirdPartySetting.appId; xiaomiAppKey.stringValue = thirdPartySetting.appKey; appSecret_in_memory = thirdPartySetting.appSecret; appId_last = xiaomiAppID.stringValue; appKey_last = xiaomiAppKey.stringValue; appSecret_last = appSecret_in_memory; } } AppStoreOnboardApi.updateRev = unityClientResp.rev; Debug.Log("Unity Client Refreshed. Finished: " + reqStruct.targetStep); AppStoreOnboardApi.loaded = true; isOperationRunning = false; serializedObject.ApplyModifiedProperties(); this.Repaint(); AssetDatabase.SaveAssets(); } else { // no client found, generate one. if (ownerAuthed) { UnityClientInfo unityClientInfo = new UnityClientInfo(); string callbackUrl = callbackUrl_in_memory; // read xiaomi from user input XiaomiSettings xiaomi = new XiaomiSettings(); xiaomi.appId = xiaomiAppID.stringValue; xiaomi.appKey = xiaomiAppKey.stringValue; xiaomi.appSecret = appSecret_in_memory; UnityWebRequest newRequest = AppStoreOnboardApi.GenerateUnityClient(Application.cloudProjectId, unityClientInfo, xiaomi, callbackUrl); UnityClientResponse clientResp = new UnityClientResponse(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = clientResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else { Debug.LogError("Permision denied."); isOperationRunning = false; } } } else if (resp.GetType() == typeof(UnityClientResponse)) { resp = JsonUtility.FromJson <UnityClientResponse> (request.downloadHandler.text); unityClientID.stringValue = ((UnityClientResponse)resp).client_id; unityClientKey.stringValue = ((UnityClientResponse)resp).client_secret; unityClientRSAPublicKey.stringValue = ((UnityClientResponse)resp).channel.publicRSAKey; clientSecret_in_memory = ((UnityClientResponse)resp).channel.channelSecret; callbackUrl_in_memory = ((UnityClientResponse)resp).channel.callbackUrl; callbackUrl_last = callbackUrl_in_memory; foreach (ThirdPartySettingsResponse thirdPartySetting in ((UnityClientResponse)resp).channel.thirdPartySettings) { if (thirdPartySetting.appType.Equals(AppStoreOnboardApi.xiaomiAppType, StringComparison.InvariantCultureIgnoreCase)) { xiaomiAppID.stringValue = thirdPartySetting.appId; xiaomiAppKey.stringValue = thirdPartySetting.appKey; appSecret_in_memory = thirdPartySetting.appSecret; appId_last = xiaomiAppID.stringValue; appKey_last = xiaomiAppKey.stringValue; appSecret_last = appSecret_in_memory; } } AppStoreOnboardApi.updateRev = ((UnityClientResponse)resp).rev; Debug.Log("Unity Client Refreshed. Finished: " + reqStruct.targetStep); AppStoreOnboardApi.loaded = true; isOperationRunning = false; serializedObject.ApplyModifiedProperties(); this.Repaint(); AssetDatabase.SaveAssets(); } } } } else { requestQueue.Enqueue(reqStruct); } }