Exemplo n.º 1
0
        private IEnumerator _getAuthCode(string scopes, OnAuthCodeCompleted complete = null, OnAuthCodeError error = null)
        {
            string _url = _oauth_uri + "get_auth_code?"; // Get Auth Code

            _url += "scope=" + scopes;                   // Set Scopes
            _url += "&client_id=" + application.app_id;  // Set Application ID
            var request = new WWW(_url);                 // Create WWW Request

            yield return(request);                       // Send Request

            // Work with Response
            if (request.error != null)  // Request has error
            {
                throw new Exception("Не удалось отправить запрос к серверу VK API. Проверьте соединение с интернетом и попробуйте снова.");
            }
            else                             // No Errors
            {
                if (request.text.Length < 1) // Error
                {
                    BaseErrorModel _error = new BaseErrorModel();
                    _error.error_code = 999;
                    _error.error_msg  = "Не удалось получить код авторизации VK API. Попробуйте изменить ваш запрос.";
                    error(_error);                                                                     // Call Error
                }
                else                                                                                   // All Right
                {
                    BaseRequestModel response = JsonUtility.FromJson <BaseRequestModel>(request.text); // Get Base Model from Response Text
                    if (response.error.error_code == -1)                                               // Response not has an errors
                    {
                        AuthCodeModel auth_code = JsonUtility.FromJson <AuthCodeModel>(request.text);  // Get Authentication Model from Response
                        if (complete != null)
                        {
                            complete(auth_code);                   // Return Complete
                        }
                    }
                    else     // Reponse has errors
                    {
                        if (error != null)
                        {
                            error(response.error);                // Show Error
                        }
                        if (settings.debug_mode)
                        {
                            Debug.Log("VK SDK Error: " + response.error.error_msg);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private IEnumerator _requestAccessToken(OnATComplete complete = null, OnATError error = null)
        {
            string _url = _oauth_uri + "code_auth_token?"; // Get Access Token URL

            _url += "device_id=" + _authCode.device_id;    // Set Device ID
            _url += "&client_id=" + application.app_id;    // Set Application ID
            var request = new WWW(_url);                   // Create WWW Request

            yield return(request);                         // Send Request

            // Work with Response
            if (request.error != null)  // Request has error
            {
                BaseErrorModel _err = new BaseErrorModel();
                _err.error_code = 999;
                _err.error_msg  = "Токен не был получен, либо авторизация еще не пройдена. Повторная попытка авторизации...";
                error(_err);
            }
            else                             // No Errors
            {
                if (request.text.Length < 1) // Error
                {
                    BaseErrorModel _err = new BaseErrorModel();
                    _err.error_code = 999;
                    _err.error_msg  = "Сервер передал пустой ответ. Повторная попытка авторизации...";
                }
                else                                                                                         // All Right
                {
                    BaseRequestModel response = JsonUtility.FromJson <BaseRequestModel>(request.text);       // Get Base Model from Response Text
                    if (response.error.error_code == -1)                                                     // Response not has an errors
                    {
                        AuthenticationModel auth = JsonUtility.FromJson <AuthenticationModel>(request.text); // Parse Auth Data
                        if (auth.access_token.Length > 0)                                                    // Has Access Token
                        {
                            authentication = auth;                                                           // Set Authentication Data
                            _isHandled     = true;                                                           // Set Handled

                            // Save Configuration
                            if (application.autosave_data)
                            {
                                SaveSDKConfigs(_auth_data_path, JsonUtility.ToJson(authentication), true);
                            }

                            // Return Done
                            complete(auth.access_token); // Return Token
                        }
                        else                             // Error
                        {
                            BaseErrorModel _err = new BaseErrorModel();
                            _err.error_code = 999;
                            _err.error_msg  = "Сервер передал ответ без токена доступа. Повторная попытка авторизации...";
                            error(_err);
                        }
                    }
                    else     // Reponse has errors
                    {
                        if (error != null)
                        {
                            error(response.error);                // Show Error
                        }
                        if (settings.debug_mode)
                        {
                            Debug.Log("VK SDK Error: " + response.error.error_msg);
                        }
                    }
                }
            }
        }