protected void oAuth2Legged()
        {
            oAuth2Legged rest = new oAuth2Legged(CLIENTID, CLIENTSECRET);

            rest.FireRequest(
                (object sender, AsyncCompletedEventArgs args) => {
                if (args == null || args.UserState == null)
                {
                    return;
                }
                if (args.Error != null)
                {
                    UnityMainThreadDispatcher.Instance().Enqueue(() => {
                        Debug.Log(ForgeLoader.GetCurrentMethod() + " " + args.Error.Message);
                    });
                    return;
                }

                //UploadValuesCompletedEventArgs args2 =args as UploadValuesCompletedEventArgs ;
                //byte[] data =args2.Result ;
                //string textData =System.Text.Encoding.UTF8.GetString (data) ;

                UploadValuesCompletedEventArgs args2 = args as UploadValuesCompletedEventArgs;
                string textData = Encoding.UTF8.GetString(args2.Result);

                JSONNode json = JSON.Parse(textData);

                BEARER = json ["access_token"];
                UnityMainThreadDispatcher.Instance().Enqueue(() => {
                    oAuthCompleted.Invoke(BEARER);
                });

                if (LOADERS != null)
                {
                    UnityMainThreadDispatcher.Instance().Enqueue(() => {
                        for (int i = 0; i < LOADERS.Count; i++)
                        {
                            GameObject loader       = LOADERS [i];
                            ForgeLoader forgeLoader = loader.GetComponent <ForgeLoader> ();
                            forgeLoader.BEARER      = BEARER;
                            if (string.IsNullOrEmpty(forgeLoader.URN) || string.IsNullOrEmpty(forgeLoader.SCENEID))
                            {
                                continue;
                            }
                            loader.SetActive(true);
                        }
                    });
                }
            }
                );
        }
        public virtual IEnumerator _FireRequest_(Action <object, AsyncCompletedEventArgs> callback = null)
        {
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
            WWWForm form = new WWWForm();

            form.AddField("client_id", CLIENT_ID);
            form.AddField("client_secret", CLIENT_SECRET);
            form.AddField("grant_type", "client_credentials");
            form.AddField("scope", "viewables:read data:read");

            using (client = UnityWebRequest.Post(uri.AbsoluteUri, form)) {
                foreach (DictionaryEntry entry in headers)
                {
                    client.SetRequestHeader(entry.Key.ToString(), entry.Value.ToString());
                }
                client.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");

                state = RequestStatus.ePending;
                //client.DownloadDataAsync (uri, this) ;
                                #if UNITY_2017_2_OR_NEWER
                yield return(client.SendWebRequest());
                                #else
                yield return(client.Send());
                                #endif

                if (client.isNetworkError || client.isHttpError)
                {
                    Debug.Log(ForgeLoader.GetCurrentMethod() + " " + client.error + " - " + client.responseCode);
                    state = RequestStatus.eError;
                }
                else
                {
                    if (callback != null)
                    {
                        UploadValuesCompletedEventArgs args = new UploadValuesCompletedEventArgs(null, false, this);
                        args.Result = client.downloadHandler.data;
                        callback(this, args);
                    }
                }
            }
        }
Beispiel #3
0
        public virtual IEnumerator _FireRequest_(Action <object, AsyncCompletedEventArgs> callback = null)
        {
            WWWForm form = new WWWForm();

            form.AddField("client_id", _CLIENT_ID);
            form.AddField("client_secret", _CLIENT_SECRET);
            form.AddField("grant_type", "client_credentials");
            form.AddField("scope", "viewables:read data:read");

            using (client = UnityWebRequest.Post(ForgeLoaderConstants._forgeoAuth2legged, form)) {
                client.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");

                state = RequestStatus.ePending;
                //client.DownloadDataAsync (new Uri (ForgeLoaderConstants._forgeoAuth2legged), this) ;
                                #if UNITY_2017_2_OR_NEWER
                yield return(client.SendWebRequest());
                                #else
                yield return(client.Send());
                                #endif

                if (client.isNetworkError || client.isHttpError)
                {
                    Debug.Log(ForgeLoader.GetCurrentMethod() + " " + client.error + " - " + client.responseCode);
                    state = RequestStatus.eError;
                }
                else
                {
                    if (callback != null)
                    {
                        UploadValuesCompletedEventArgs args = new UploadValuesCompletedEventArgs(null, false, this);
                        args.Result = client.downloadHandler.data;
                        callback(this, args);
                    }
                }
            }
        }
Beispiel #4
0
        public virtual IEnumerator _FireRequest_(Action <object, AsyncCompletedEventArgs> callback = null)
        {
            WWWForm form = new WWWForm();

            form.AddField("client_id", ForgeLoaderConstants.FORGE_CLIENT_ID);
            form.AddField("client_secret", ForgeLoaderConstants.FORGE_CLIENT_SECRET);
            form.AddField("grant_type", "client_credentials");
            form.AddField("scope", "viewables:read data:read");

            using (client = UnityWebRequest.Post(uri.AbsoluteUri, form)) {
                foreach (DictionaryEntry entry in headers)
                {
                    client.SetRequestHeader(entry.Key.ToString(), entry.Value.ToString());
                }
                client.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");

                state = RequestStatus.ePending;
                //client.DownloadDataAsync (uri, this) ;
                yield return(client.Send());

                if (client.isNetworkError /*client.isNetworkError*/)
                {
                    Debug.Log(ForgeLoader.GetCurrentMethod() + " " + client.error);
                    state = RequestStatus.eError;
                }
                else
                {
                    if (callback != null)
                    {
                        UploadValuesCompletedEventArgs args = new UploadValuesCompletedEventArgs(null, false, this);
                        args.Result = client.downloadHandler.data;
                        callback(this, args);
                    }
                }
            }
        }
Beispiel #5
0
        public override void Refresh()
        {
            if (string.IsNullOrEmpty(_CLIENT_ID) || string.IsNullOrEmpty(_CLIENT_SECRET))
            {
                return;
            }

            FireRequest(
                (object sender, AsyncCompletedEventArgs args) => {
                if (args == null || args.UserState == null)
                {
                    return;
                }
                if (args.Error != null)
                {
                    UnityMainThreadDispatcher.Instance().Enqueue(() => {
                        Debug.Log(ForgeLoader.GetCurrentMethod() + " " + args.Error.Message);
                    });
                    return;
                }

                                        #if !UNITY_WSA
                UploadStringCompletedEventArgs args2 = args as UploadStringCompletedEventArgs;
                string textData = args2.Result;
                                        #else
                UploadValuesCompletedEventArgs args2 = args as UploadValuesCompletedEventArgs;
                string textData = System.Text.Encoding.UTF8.GetString(args2.Result);
                                        #endif

                try {
                    credentials = JSONNode.Parse(textData);
                    expiresAt   = DateTime.Now + TimeSpan.FromSeconds(credentials ["expires_in"].AsDouble - 120 /*2 minutes*/);
                    if (isActive)
                    {
                        _timer = new System.Threading.Timer((obj) => {
                            UnityMainThreadDispatcher.Instance().Enqueue(() => {
                                Refresh();
                            });
                        },
                                                            null,
                                                            Math.Max(2500, (int)(TimeSpan.FromSeconds(credentials ["expires_in"].AsDouble - 120 /*2 minutes*/).TotalMilliseconds)),
                                                            System.Threading.Timeout.Infinite
                                                            );
                    }

                    UnityMainThreadDispatcher.Instance().Enqueue(() => {
                        _CredentialsReceived.Invoke(credentials ["access_token"].Value);

                        for (int i = 0; _ForgeLoaders != null && i < _ForgeLoaders.Count; i++)
                        {
                            GameObject loader       = _ForgeLoaders [i];
                            ForgeLoader forgeLoader = loader.GetComponent <ForgeLoader> ();
                            if (forgeLoader == null)
                            {
                                continue;
                            }
                            forgeLoader._BEARER = credentials ["access_token"].Value;
                            if (string.IsNullOrEmpty(forgeLoader.URN) || string.IsNullOrEmpty(forgeLoader.SCENEID))
                            {
                                continue;
                            }
                            loader.SetActive(true);
                        }
                    });
                } catch (Exception ex) {
                    UnityMainThreadDispatcher.Instance().Enqueue(() => {
                        Debug.Log(ForgeLoader.GetCurrentMethod() + " " + ex.Message);
                    });
                }
            }
                );
        }