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.
 
 
 

161 lines
5.8 KiB

  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. #if UNITY_2017_2_OR_NEWER
  43. request.SendWebRequest ();
  44. #else
  45. request.Send ();
  46. #endif
  47. return request;
  48. }
  49. public static UnityWebRequest RefreshToken() {
  50. TokenRequest req = new TokenRequest ();
  51. req.client_id = oauthClientId;
  52. req.client_secret = oauthClientSecret;
  53. req.grant_type = "refresh_token";
  54. req.refresh_token = tokenInfo.refresh_token;
  55. return asyncRequest (UnityWebRequest.kHttpVerbPOST, url, "/v1/oauth2/token", null, req);
  56. }
  57. public static UnityWebRequest GetAccessToken(string authCode) {
  58. TokenRequest req = new TokenRequest ();
  59. req.code = authCode;
  60. req.client_id = oauthClientId;
  61. req.client_secret = oauthClientSecret;
  62. req.grant_type = "authorization_code";
  63. req.redirect_uri = oauthRedirectUri;
  64. return asyncRequest (UnityWebRequest.kHttpVerbPOST, AppStoreOnboardApi.url, "/v1/oauth2/token", null, req);
  65. }
  66. public static UnityWebRequest GetUserId() {
  67. string token = tokenInfo.access_token;
  68. string api = "/v1/oauth2/tokeninfo?access_token=" + token;
  69. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  70. }
  71. public static UnityWebRequest GetOrgId(string projectGuid) {
  72. string api = "/v1/core/api/projects/" + projectGuid;
  73. string token = tokenInfo.access_token;
  74. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  75. }
  76. public static UnityWebRequest GetOrgRoles() {
  77. string api = "/v1/organizations/" + orgId + "/roles?userId=" + userId;
  78. string token = tokenInfo.access_token;
  79. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  80. }
  81. public static UnityWebRequest GetUnityClientInfo(string projectGuid) {
  82. string api = "/v1/oauth2/user-clients?projectGuid=" + projectGuid;
  83. string token = tokenInfo.access_token;
  84. return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
  85. }
  86. public static UnityWebRequest GenerateUnityClient(string projectGuid, UnityClientInfo unityClientInfo, XiaomiSettings xiaomi, string callbackUrl) {
  87. return generateOrUpdateUnityClient (projectGuid, UnityWebRequest.kHttpVerbPOST, unityClientInfo, xiaomi, callbackUrl);
  88. }
  89. public static UnityWebRequest UpdateUnityClient(string projectGuid, UnityClientInfo unityClientInfo, XiaomiSettings xiaomi, string callbackUrl) {
  90. return generateOrUpdateUnityClient (projectGuid, UnityWebRequest.kHttpVerbPUT, unityClientInfo, xiaomi, callbackUrl);
  91. }
  92. static UnityWebRequest generateOrUpdateUnityClient(string projectGuid, string method, UnityClientInfo unityClientInfo, XiaomiSettings xiaomi, string callbackUrl) {
  93. // TODO read xiaomi info from user input
  94. UnityChannel channel = new UnityChannel ();
  95. channel.xiaomi = xiaomi;
  96. channel.projectGuid = projectGuid;
  97. channel.callbackUrl = callbackUrl;
  98. // set necessary client post data
  99. UnityClient client = new UnityClient ();
  100. client.client_name = projectGuid;
  101. client.scopes.Add ("identity");
  102. client.channel = channel;
  103. string api = null;
  104. if (method.Equals (UnityWebRequest.kHttpVerbPOST, StringComparison.InvariantCultureIgnoreCase)) {
  105. api = "/v1/oauth2/user-clients";
  106. } else if (method.Equals (UnityWebRequest.kHttpVerbPUT, StringComparison.InvariantCultureIgnoreCase)) {
  107. // if client is not generated or loaded, directly ignore update
  108. if (unityClientInfo.ClientId == null) {
  109. Debug.LogError ("Please get/generate Unity Client first.");
  110. loaded = false;
  111. return null;
  112. }
  113. if (updateRev == null) {
  114. Debug.LogError ("Please get/generate Unity Client first.");
  115. loaded = false;
  116. return null;
  117. }
  118. client.rev = updateRev;
  119. if (orgId == null) {
  120. Debug.LogError ("Please get/generate Unity Client first.");
  121. loaded = false;
  122. return null;
  123. }
  124. client.owner = orgId;
  125. client.ownerType = "ORGANIZATION";
  126. api = "/v1/oauth2/user-clients/" + unityClientInfo.ClientId;
  127. } else {
  128. return null;
  129. }
  130. string token = tokenInfo.access_token;
  131. return asyncRequest(method, url, api, token, client);
  132. }
  133. public static UnityWebRequest UpdateUnityClientSecret(string clientId) {
  134. if (clientId == null) {
  135. return null;
  136. }
  137. string token = tokenInfo.access_token;
  138. return asyncRequest(UnityWebRequest.kHttpVerbPUT, url, "/v1/oauth2/user-clients/channel-secret?clientId=" + clientId, token, null);
  139. }
  140. }
  141. }
  142. #endif