public static UnityWebRequest UpdateUnityClient(string projectGuid, UnityClientInfo unityClientInfo, XiaomiSettings xiaomi, string callbackUrl) { return(generateOrUpdateUnityClient(projectGuid, UnityWebRequest.kHttpVerbPUT, unityClientInfo, xiaomi, callbackUrl)); }
static UnityWebRequest generateOrUpdateUnityClient(string projectGuid, string method, UnityClientInfo unityClientInfo, XiaomiSettings xiaomi, string callbackUrl) { // TODO read xiaomi info from user input UnityChannel channel = new UnityChannel(); channel.xiaomi = xiaomi; channel.projectGuid = projectGuid; channel.callbackUrl = callbackUrl; // set necessary client post data UnityClient client = new UnityClient(); client.client_name = projectGuid; client.scopes.Add("identity"); client.channel = channel; string api = null; if (method.Equals(UnityWebRequest.kHttpVerbPOST, StringComparison.InvariantCultureIgnoreCase)) { api = "/v1/oauth2/user-clients"; } else if (method.Equals(UnityWebRequest.kHttpVerbPUT, StringComparison.InvariantCultureIgnoreCase)) { // if client is not generated or loaded, directly ignore update if (unityClientInfo.ClientId == null) { Debug.LogError("Please get/generate Unity Client first."); loaded = false; return(null); } if (updateRev == null) { Debug.LogError("Please get/generate Unity Client first."); loaded = false; return(null); } client.rev = updateRev; if (orgId == null) { Debug.LogError("Please get/generate Unity Client first."); loaded = false; return(null); } client.owner = orgId; client.ownerType = "ORGANIZATION"; api = "/v1/oauth2/user-clients/" + unityClientInfo.ClientId; } else { return(null); } string token = tokenInfo.access_token; return(asyncRequest(method, url, api, token, client)); }
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); } }