/// <summary> /// Upload new RemoteConfigData via REST API. /// </summary> /// <param name="newData">New data to upload to Remote Config.</param> private static void UpdateRemoteConfig(RemoteConfigData newData) { var newRemoteConfigData = JsonConvert.SerializeObject(newData); var webRequest = UnityWebRequest.Put(remoteConfigUrl, newRemoteConfigData); webRequest.SetRequestHeader("Authorization", "Bearer " + accessToken); webRequest.SetRequestHeader("Content-Type", "application/json; UTF8"); webRequest.SetRequestHeader("If-Match", "*"); string data = ""; webRequest.SendWebRequest().completed += op => { data = webRequest.downloadHandler.text; if (webRequest.isHttpError) { var exception = new Exception( $"Error uploading RemoteConfig: {webRequest.error}\nData: {data}"); RetrieveError.Invoke(null, new ErrorArgs(exception)); return; } SetCurrentData(data); DataUploaded(CurrentData, CurrentData); }; }
/// <summary> /// Retrieves an access token from a GoogleCredential, then triggers an optional callback. /// </summary> /// <param name="andThen">Optional callback to invoke after access token is retrieved.</param> private static void GetAccessToken(Action andThen = null) { // Only retrieve a new access token if one hasn't already been retrieved. if (string.IsNullOrWhiteSpace(accessToken)) { if (!TryGetGoogleCredential(out var credential)) { RetrieveError?.Invoke(null, new ErrorArgs( new Exception("Invalid google credential file."), invalidCreds: true)); return; } credential.CreateScoped(remoteConfigScope) .UnderlyingCredential .GetAccessTokenForRequestAsync(oauthUrl) .ContinueWith(task => { if (task.Status == TaskStatus.Faulted) { accessToken = ""; var exception = new Exception( $"Exception retrieving access token from Firebase.", task.Exception); RetrieveError?.Invoke(null, new ErrorArgs(exception)); return; } accessToken = task.Result; if (string.IsNullOrWhiteSpace(accessToken)) { RetrieveError?.Invoke(null, new ErrorArgs(new ArgumentNullException( "Empty access token retrieved from Firebase."))); return; } if (andThen != null) { QueueCallback(andThen); } }); } else if (andThen != null) { QueueCallback(andThen); } }