You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

362 lines
15 KiB

  1. using System;
  2. using System.Reflection;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using UnityEngine.Networking;
  6. namespace UnityEngine.UDP.Editor
  7. {
  8. public class AppStoreOnboardApi
  9. {
  10. public const string oauthClientId = BuildConfig.CLIENT_ID;
  11. public const string oauthClientSecret = BuildConfig.CLIENT_SECRET; //staging
  12. public const string oauthRedirectUri = BuildConfig.ID_ENDPOINT;
  13. public const string url = BuildConfig.API_ENDPOINT;
  14. public const string expiredAccessTokenInfo = "Expired Access Token";
  15. public const string invalidAccessTokenInfo = "Invalid Access Token";
  16. public const string expiredRefreshTokenInfo = "Expired Refresh Token";
  17. public const string invalidRefreshTokenInfo = "Invalid Refresh Token";
  18. public const string forbiddenInfo = "Forbidden";
  19. public static TokenInfo tokenInfo = new TokenInfo();
  20. public static ThirdPartySetting[] tps = new ThirdPartySetting[10];
  21. public static string userId;
  22. public static string orgId;
  23. public static string updateRev;
  24. public static bool loaded = false;
  25. public const string udpurl = BuildConfig.UDP_ENDPOINT;
  26. public static UnityWebRequest asyncRequest(string method, string url, string api, string token,
  27. object postObject)
  28. {
  29. UnityWebRequest request = new UnityWebRequest(url + api, method);
  30. if (postObject != null)
  31. {
  32. string postData = HandlePostData(JsonUtility.ToJson(postObject));
  33. byte[] postDataBytes = Encoding.UTF8.GetBytes(postData);
  34. request.uploadHandler = (UploadHandler) new UploadHandlerRaw(postDataBytes);
  35. }
  36. request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
  37. // set content-type header
  38. request.SetRequestHeader("Content-Type", "application/json");
  39. // set auth header
  40. if (token != null)
  41. {
  42. request.SetRequestHeader("Authorization", "Bearer " + token);
  43. }
  44. MethodInfo sendWebRequest = request.GetType().GetMethod("SendWebRequest");
  45. if (sendWebRequest == null)
  46. {
  47. sendWebRequest = request.GetType().GetMethod("Send");
  48. }
  49. sendWebRequest.Invoke(request, null);
  50. return request;
  51. }
  52. private static string HandlePostData(string oldData)
  53. {
  54. string newData = oldData.Replace("thisShouldBeENHyphenUS", "en-US");
  55. newData = newData.Replace("thisShouldBeZHHyphenCN", "zh-CN");
  56. Regex re = new Regex("\"\\w+?\":\"\",");
  57. newData = re.Replace(newData, "");
  58. re = new Regex(",\"\\w+?\":\"\"");
  59. newData = re.Replace(newData, "");
  60. re = new Regex("\"\\w+?\":\"\"");
  61. newData = re.Replace(newData, "");
  62. return newData;
  63. }
  64. public static UnityWebRequest asyncRequest(string method, string url, string api, string token,
  65. object postObject, bool isTest)
  66. {
  67. UnityWebRequest request = new UnityWebRequest(url + api, method);
  68. if (postObject != null)
  69. {
  70. string postData = HandlePostData(JsonUtility.ToJson(postObject));
  71. byte[] postDataBytes = Encoding.UTF8.GetBytes(postData);
  72. request.uploadHandler = (UploadHandler) new UploadHandlerRaw(postDataBytes);
  73. }
  74. request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
  75. // set content-type header
  76. request.SetRequestHeader("Content-Type", "application/json");
  77. // set auth header
  78. if (isTest)
  79. {
  80. }
  81. else if (token != null)
  82. {
  83. request.SetRequestHeader("Authorization", "Bearer " + token);
  84. }
  85. MethodInfo sendWebRequestMethodInfo = request.GetType().GetMethod("SendWebRequest");
  86. if (sendWebRequestMethodInfo == null)
  87. {
  88. sendWebRequestMethodInfo = request.GetType().GetMethod("Send");
  89. }
  90. sendWebRequestMethodInfo.Invoke(request, null);
  91. return request;
  92. }
  93. public static UnityWebRequest RefreshToken()
  94. {
  95. TokenRequest req = new TokenRequest();
  96. req.client_id = oauthClientId;
  97. req.client_secret = oauthClientSecret;
  98. req.grant_type = "refresh_token";
  99. req.refresh_token = tokenInfo.refresh_token;
  100. return asyncRequest(UnityWebRequest.kHttpVerbPOST, url, "/v1/oauth2/token", null, req);
  101. }
  102. public static UnityWebRequest GetAccessToken(string authCode)
  103. {
  104. TokenRequest req = new TokenRequest();
  105. req.code = authCode;
  106. req.client_id = oauthClientId;
  107. req.client_secret = oauthClientSecret;
  108. req.grant_type = "authorization_code";
  109. req.redirect_uri = oauthRedirectUri;
  110. return asyncRequest(UnityWebRequest.kHttpVerbPOST, url, "/v1/oauth2/token", null, req);
  111. }
  112. public static UnityWebRequest GetUserId()
  113. {
  114. string token = tokenInfo.access_token;
  115. string api = "/v1/oauth2/tokeninfo?access_token=" + token;
  116. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  117. }
  118. public static UnityWebRequest GetOrgId(string projectGuid)
  119. {
  120. string api = "/v1/core/api/projects/" + projectGuid;
  121. string token = tokenInfo.access_token;
  122. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  123. }
  124. public static UnityWebRequest GetOrgRoles()
  125. {
  126. string api = "/v1/organizations/" + orgId + "/roles?userId=" + userId;
  127. string token = tokenInfo.access_token;
  128. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  129. }
  130. public static UnityWebRequest GetUnityClientInfo(string projectGuid)
  131. {
  132. string api = "/v1/oauth2/user-clients?projectGuid=" + projectGuid;
  133. string token = tokenInfo.access_token;
  134. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  135. }
  136. public static UnityWebRequest GetUnityClientInfoByClientId(string clientId)
  137. {
  138. string api = "/v1/oauth2/user-clients/" + clientId;
  139. string token = tokenInfo.access_token;
  140. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  141. }
  142. public static UnityWebRequest GenerateUnityClient(string projectGuid, UnityClientInfo unityClientInfo,
  143. string callbackUrl)
  144. {
  145. return generateOrUpdateUnityClient(projectGuid, UnityWebRequest.kHttpVerbPOST, unityClientInfo,
  146. callbackUrl);
  147. }
  148. public static UnityWebRequest UpdateUnityClient(string projectGuid, UnityClientInfo unityClientInfo,
  149. string callbackUrl)
  150. {
  151. return generateOrUpdateUnityClient(projectGuid, UnityWebRequest.kHttpVerbPUT, unityClientInfo, callbackUrl);
  152. }
  153. static UnityWebRequest generateOrUpdateUnityClient(string projectGuid, string method,
  154. UnityClientInfo unityClientInfo, string callbackUrl)
  155. {
  156. UnityChannel channel = new UnityChannel();
  157. channel.projectGuid = projectGuid;
  158. channel.callbackUrl = callbackUrl;
  159. if (tps != null && tps.Length > 0 && tps[0] != null && !String.IsNullOrEmpty(tps[0].appId))
  160. {
  161. channel.thirdPartySettings = tps;
  162. for (int i = 0; i < channel.thirdPartySettings.Length; i++)
  163. {
  164. if (channel.thirdPartySettings[i].appType.Equals("gstore"))
  165. {
  166. channel.thirdPartySettings[i].appKey = null;
  167. channel.thirdPartySettings[i].appSecret = null;
  168. }
  169. if (channel.thirdPartySettings[i].appType.Equals("xiaomi"))
  170. {
  171. channel.thirdPartySettings[i].extraProperties = null;
  172. }
  173. }
  174. }
  175. // set necessary client post data
  176. UnityClient client = new UnityClient();
  177. client.client_name = projectGuid;
  178. client.scopes.Add("identity");
  179. client.channel = channel;
  180. string api = null;
  181. if (method.Equals(UnityWebRequest.kHttpVerbPOST, StringComparison.InvariantCultureIgnoreCase))
  182. {
  183. api = "/v1/oauth2/user-clients";
  184. }
  185. else if (method.Equals(UnityWebRequest.kHttpVerbPUT, StringComparison.InvariantCultureIgnoreCase))
  186. {
  187. // if client is not generated or loaded, directly ignore update
  188. if (unityClientInfo.ClientId == null)
  189. {
  190. Debug.LogError("Please get/generate Unity Client first.");
  191. loaded = false;
  192. return null;
  193. }
  194. if (updateRev == null)
  195. {
  196. Debug.LogError("Please get/generate Unity Client first.");
  197. loaded = false;
  198. return null;
  199. }
  200. client.rev = updateRev;
  201. if (orgId == null)
  202. {
  203. Debug.LogError("Please get/generate Unity Client first.");
  204. loaded = false;
  205. return null;
  206. }
  207. client.owner = orgId;
  208. client.ownerType = "ORGANIZATION";
  209. api = "/v1/oauth2/user-clients/" + unityClientInfo.ClientId;
  210. }
  211. else
  212. {
  213. return null;
  214. }
  215. string token = tokenInfo.access_token;
  216. return asyncRequest(method, url, api, token, client);
  217. }
  218. public static UnityWebRequest UpdateUnityClientSecret(string clientId)
  219. {
  220. if (clientId == null)
  221. {
  222. return null;
  223. }
  224. string token = tokenInfo.access_token;
  225. return asyncRequest(UnityWebRequest.kHttpVerbPUT, url,
  226. "/v1/oauth2/user-clients/channel-secret?clientId=" + clientId, token, null);
  227. }
  228. public static UnityWebRequest SaveTestAccount(Player player, string clientId)
  229. {
  230. string api = "/v1/player";
  231. string token = tokenInfo.access_token;
  232. player.clientId = clientId;
  233. return asyncRequest(UnityWebRequest.kHttpVerbPOST, udpurl, api, token, player, false);
  234. }
  235. public static UnityWebRequest VerifyTestAccount(string playerId)
  236. {
  237. string api = "/v1/player/" + playerId + "/set-email-verified";
  238. string token = tokenInfo.access_token;
  239. return asyncRequest(UnityWebRequest.kHttpVerbPOST, udpurl, api, token, null, false);
  240. }
  241. public static UnityWebRequest GetTestAccount(string clientId)
  242. {
  243. string api = "/v1/player/0/all?clientId=" + clientId;
  244. string token = tokenInfo.access_token;
  245. return asyncRequest(UnityWebRequest.kHttpVerbGET, udpurl, api, token, null, false);
  246. }
  247. public static UnityWebRequest DeleteTestAccount(string playerId)
  248. {
  249. string api = "/v1/player/" + playerId;
  250. string token = tokenInfo.access_token;
  251. return asyncRequest(UnityWebRequest.kHttpVerbDELETE, udpurl, api, token, null, false);
  252. }
  253. public static UnityWebRequest UpdateTestAccount(PlayerChangePasswordRequest player)
  254. {
  255. string api = "/v1/player/change-password";
  256. string token = tokenInfo.access_token;
  257. return asyncRequest(UnityWebRequest.kHttpVerbPOST, udpurl, api, token, player, false);
  258. }
  259. public static UnityWebRequest CreateAppItem(AppItem appItem)
  260. {
  261. string api = "/v1/store/items";
  262. string token = tokenInfo.access_token;
  263. appItem.status = "STAGE";
  264. appItem.ownerType = "ORGANIZATION";
  265. appItem.type = "APP";
  266. appItem.packageName = "com.unity";
  267. return asyncRequest(UnityWebRequest.kHttpVerbPOST, udpurl, api, token, appItem, false);
  268. }
  269. public static UnityWebRequest UpdateAppItem(AppItem appItem)
  270. {
  271. string api = "/v1/store/items/" + appItem.id;
  272. string token = tokenInfo.access_token;
  273. return asyncRequest(UnityWebRequest.kHttpVerbPUT, udpurl, api, token, appItem, false);
  274. }
  275. public static UnityWebRequest PublishAppItem(string appItemId)
  276. {
  277. string api = "/v1/store/items/" + appItemId + "/listing";
  278. string token = tokenInfo.access_token;
  279. return asyncRequest(UnityWebRequest.kHttpVerbPOST, udpurl, api, token, null, false);
  280. }
  281. public static UnityWebRequest GetAppItem(string clientId)
  282. {
  283. string api = "/v1/store/items/search?ownerId=" + orgId +
  284. "&ownerType=ORGANIZATION&type=APP&start=0&count=1&clientId=" + clientId;
  285. string token = tokenInfo.access_token;
  286. return asyncRequest(UnityWebRequest.kHttpVerbGET, udpurl, api, token, null, false);
  287. }
  288. public static UnityWebRequest CreateStoreItem(IapItem iapItem)
  289. {
  290. string api = "/v1/store/items";
  291. string token = tokenInfo.access_token;
  292. iapItem.ownerId = orgId;
  293. return asyncRequest(UnityWebRequest.kHttpVerbPOST, udpurl, api, token, iapItem, false);
  294. }
  295. public static UnityWebRequest UpdateStoreItem(IapItem iapItem)
  296. {
  297. string api = "/v1/store/items/" + iapItem.id;
  298. string token = tokenInfo.access_token;
  299. return asyncRequest(UnityWebRequest.kHttpVerbPUT, udpurl, api, token, iapItem, false);
  300. }
  301. public static UnityWebRequest SearchStoreItem(String appItemSlug)
  302. {
  303. string api = "/v1/store/items/search?ownerId=" + orgId +
  304. "&ownerType=ORGANIZATION&start=0&count=20&type=IAP&masterItemSlug=" + appItemSlug;
  305. string token = tokenInfo.access_token;
  306. return asyncRequest(UnityWebRequest.kHttpVerbGET, udpurl, api, token, null, false);
  307. }
  308. public static UnityWebRequest DeleteStoreItem(string iapItemId)
  309. {
  310. string api = "/v1/store/items/" + iapItemId;
  311. string token = tokenInfo.access_token;
  312. return asyncRequest(UnityWebRequest.kHttpVerbDELETE, udpurl, api, token, null, false);
  313. }
  314. public static UnityWebRequest GetAppItemSlugWithId(String appItemId)
  315. {
  316. string api = "/v1/store/items/" + appItemId;
  317. string token = tokenInfo.access_token;
  318. return asyncRequest(UnityWebRequest.kHttpVerbGET, udpurl, api, token, null, false);
  319. }
  320. }
  321. }