AppStoreOnboardApi.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #if UNITY_5_6_OR_NEWER && !UNITY_5_6_0
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using UnityEditor.Connect;
  7. using UnityEngine.Networking;
  8. using System.Text;
  9. using System.Threading;
  10. using System;
  11. using AppStoreModel;
  12. namespace AppStoresSupport
  13. {
  14. public class AppStoreOnboardApi
  15. {
  16. public const string oauthClientId = "channel_editor";
  17. public const string oauthClientSecret = "B63AFB324DE3D12A13827340019D1EE3";
  18. public const string oauthRedirectUri = "https://id.unity.com";
  19. public const string url = "https://api.unity.com";
  20. public const string xiaomiAppType = "xiaomi";
  21. public const string tokenExpiredInfo = "Expired Access Token";
  22. public static TokenInfo tokenInfo = new TokenInfo();
  23. public static string userId;
  24. public static string orgId;
  25. public static string updateRev;
  26. public static bool loaded = false;
  27. public static UnityWebRequest asyncRequest(string method, string url, string api, string token, object postObject)
  28. {
  29. UnityWebRequest request = new UnityWebRequest(url + api, method);
  30. if (postObject != null) {
  31. string postData = JsonUtility.ToJson (postObject);
  32. byte[] postDataBytes = Encoding.UTF8.GetBytes (postData);
  33. request.uploadHandler = (UploadHandler)new UploadHandlerRaw (postDataBytes);
  34. }
  35. request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
  36. // set content-type header
  37. request.SetRequestHeader ("Content-Type", "application/json");
  38. // set auth header
  39. if (token != null) {
  40. request.SetRequestHeader ("Authorization", "Bearer " + token);
  41. }
  42. request.Send ();
  43. return request;
  44. }
  45. public static UnityWebRequest RefreshToken() {
  46. TokenRequest req = new TokenRequest ();
  47. req.client_id = oauthClientId;
  48. req.client_secret = oauthClientSecret;
  49. req.grant_type = "refresh_token";
  50. req.refresh_token = tokenInfo.refresh_token;
  51. return asyncRequest (UnityWebRequest.kHttpVerbPOST, url, "/v1/oauth2/token", null, req);
  52. }
  53. public static UnityWebRequest GetAccessToken(string authCode) {
  54. TokenRequest req = new TokenRequest ();
  55. req.code = authCode;
  56. req.client_id = oauthClientId;
  57. req.client_secret = oauthClientSecret;
  58. req.grant_type = "authorization_code";
  59. req.redirect_uri = oauthRedirectUri;
  60. return asyncRequest (UnityWebRequest.kHttpVerbPOST, AppStoreOnboardApi.url, "/v1/oauth2/token", null, req);
  61. }
  62. public static UnityWebRequest GetUserId() {
  63. string token = tokenInfo.access_token;
  64. string api = "/v1/oauth2/tokeninfo?access_token=" + token;
  65. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  66. }
  67. public static UnityWebRequest GetOrgId(string projectGuid) {
  68. string api = "/v1/core/api/projects/" + projectGuid;
  69. string token = tokenInfo.access_token;
  70. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  71. }
  72. public static UnityWebRequest GetOrgRoles() {
  73. string api = "/v1/organizations/" + orgId + "/roles?userId=" + userId;
  74. string token = tokenInfo.access_token;
  75. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  76. }
  77. public static UnityWebRequest GetUnityClientInfo(string projectGuid) {
  78. string api = "/v1/oauth2/user-clients?projectGuid=" + projectGuid;
  79. string token = tokenInfo.access_token;
  80. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  81. }
  82. public static UnityWebRequest GenerateUnityClient(string projectGuid, UnityClientInfo unityClientInfo, XiaomiSettings xiaomi, string callbackUrl) {
  83. return generateOrUpdateUnityClient (projectGuid, UnityWebRequest.kHttpVerbPOST, unityClientInfo, xiaomi, callbackUrl);
  84. }
  85. public static UnityWebRequest UpdateUnityClient(string projectGuid, UnityClientInfo unityClientInfo, XiaomiSettings xiaomi, string callbackUrl) {
  86. return generateOrUpdateUnityClient (projectGuid, UnityWebRequest.kHttpVerbPUT, unityClientInfo, xiaomi, callbackUrl);
  87. }
  88. static UnityWebRequest generateOrUpdateUnityClient(string projectGuid, string method, UnityClientInfo unityClientInfo, XiaomiSettings xiaomi, string callbackUrl) {
  89. // TODO read xiaomi info from user input
  90. UnityChannel channel = new UnityChannel ();
  91. channel.xiaomi = xiaomi;
  92. channel.projectGuid = projectGuid;
  93. channel.callbackUrl = callbackUrl;
  94. // set necessary client post data
  95. UnityClient client = new UnityClient ();
  96. client.client_name = projectGuid;
  97. client.scopes.Add ("identity");
  98. client.channel = channel;
  99. string api = null;
  100. if (method.Equals (UnityWebRequest.kHttpVerbPOST, StringComparison.InvariantCultureIgnoreCase)) {
  101. api = "/v1/oauth2/user-clients";
  102. } else if (method.Equals (UnityWebRequest.kHttpVerbPUT, StringComparison.InvariantCultureIgnoreCase)) {
  103. // if client is not generated or loaded, directly ignore update
  104. if (unityClientInfo.ClientId == null) {
  105. Debug.LogError ("Please get/generate Unity Client first.");
  106. loaded = false;
  107. return null;
  108. }
  109. if (updateRev == null) {
  110. Debug.LogError ("Please get/generate Unity Client first.");
  111. loaded = false;
  112. return null;
  113. }
  114. client.rev = updateRev;
  115. if (orgId == null) {
  116. Debug.LogError ("Please get/generate Unity Client first.");
  117. loaded = false;
  118. return null;
  119. }
  120. client.owner = orgId;
  121. client.ownerType = "ORGANIZATION";
  122. api = "/v1/oauth2/user-clients/" + unityClientInfo.ClientId;
  123. } else {
  124. return null;
  125. }
  126. string token = tokenInfo.access_token;
  127. return asyncRequest(method, url, api, token, client);
  128. }
  129. public static UnityWebRequest UpdateUnityClientSecret(string clientId) {
  130. if (clientId == null) {
  131. return null;
  132. }
  133. string token = tokenInfo.access_token;
  134. return asyncRequest(UnityWebRequest.kHttpVerbPUT, url, "/v1/oauth2/user-clients/channel-secret?clientId=" + clientId, token, null);
  135. }
  136. }
  137. }
  138. #endif