/
AppStoreOnboardApi.cs
156 lines (138 loc) · 5.73 KB
/
AppStoreOnboardApi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#if UNITY_5_6_OR_NEWER && !UNITY_5_6_0
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Connect;
using UnityEngine.Networking;
using System.Text;
using System.Threading;
using System;
using AppStoreModel;
namespace AppStoresSupport
{
public class AppStoreOnboardApi
{
public const string oauthClientId = "channel_editor";
public const string oauthClientSecret = "B63AFB324DE3D12A13827340019D1EE3";
public const string oauthRedirectUri = "https://id.unity.com";
public const string url = "https://api.unity.com";
public const string xiaomiAppType = "xiaomi";
public const string tokenExpiredInfo = "Expired Access Token";
public static TokenInfo tokenInfo = new TokenInfo();
public static string userId;
public static string orgId;
public static string updateRev;
public static bool loaded = false;
public static UnityWebRequest asyncRequest(string method, string url, string api, string token, object postObject)
{
UnityWebRequest request = new UnityWebRequest(url + api, method);
if (postObject != null) {
string postData = JsonUtility.ToJson (postObject);
byte[] postDataBytes = Encoding.UTF8.GetBytes (postData);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw (postDataBytes);
}
request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
// set content-type header
request.SetRequestHeader ("Content-Type", "application/json");
// set auth header
if (token != null) {
request.SetRequestHeader ("Authorization", "Bearer " + token);
}
request.Send ();
return request;
}
public static UnityWebRequest RefreshToken() {
TokenRequest req = new TokenRequest ();
req.client_id = oauthClientId;
req.client_secret = oauthClientSecret;
req.grant_type = "refresh_token";
req.refresh_token = tokenInfo.refresh_token;
return asyncRequest (UnityWebRequest.kHttpVerbPOST, url, "/v1/oauth2/token", null, req);
}
public static UnityWebRequest GetAccessToken(string authCode) {
TokenRequest req = new TokenRequest ();
req.code = authCode;
req.client_id = oauthClientId;
req.client_secret = oauthClientSecret;
req.grant_type = "authorization_code";
req.redirect_uri = oauthRedirectUri;
return asyncRequest (UnityWebRequest.kHttpVerbPOST, AppStoreOnboardApi.url, "/v1/oauth2/token", null, req);
}
public static UnityWebRequest GetUserId() {
string token = tokenInfo.access_token;
string api = "/v1/oauth2/tokeninfo?access_token=" + token;
return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
}
public static UnityWebRequest GetOrgId(string projectGuid) {
string api = "/v1/core/api/projects/" + projectGuid;
string token = tokenInfo.access_token;
return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
}
public static UnityWebRequest GetOrgRoles() {
string api = "/v1/organizations/" + orgId + "/roles?userId=" + userId;
string token = tokenInfo.access_token;
return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
}
public static UnityWebRequest GetUnityClientInfo(string projectGuid) {
string api = "/v1/oauth2/user-clients?projectGuid=" + projectGuid;
string token = tokenInfo.access_token;
return asyncRequest(UnityWebRequest.kHttpVerbGET, url, api, token, null);
}
public static UnityWebRequest GenerateUnityClient(string projectGuid, UnityClientInfo unityClientInfo, XiaomiSettings xiaomi, string callbackUrl) {
return generateOrUpdateUnityClient (projectGuid, UnityWebRequest.kHttpVerbPOST, unityClientInfo, xiaomi, callbackUrl);
}
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);
}
public static UnityWebRequest UpdateUnityClientSecret(string clientId) {
if (clientId == null) {
return null;
}
string token = tokenInfo.access_token;
return asyncRequest(UnityWebRequest.kHttpVerbPUT, url, "/v1/oauth2/user-clients/channel-secret?clientId=" + clientId, token, null);
}
}
}
#endif