Пример #1
0
        private void DoDispatch <T>(
            Dictionary <string, string> parametersDict,
            Action <VKBackendResult <T> > callback,
            Func <string, T> customDeserializationFunc = null)
        {
            parametersDict["v"] = VKSDK.API_VERSION;

            var accessToken = VKSDK.GetAccessToken();

            if (accessToken != null)
            {
                parametersDict["access_token"] = accessToken.AccessToken;
            }

            var dispatchUri = string.Format(REQUEST_BASE_URI_FRM, _parameters.MethodName);

            VKHttpRequestHelper.DispatchHTTPRequest(
                dispatchUri,
                parametersDict,
                (httpResult) =>
            {
                if (httpResult.IsSucceeded)
                {
                    var backendResult = GetBackendResultFromString <T>(httpResult.Data, customDeserializationFunc);


                    if (backendResult.ResultCode == VKResultCode.CaptchaRequired)
                    {
                        var captchaRequest = new VKCaptchaUserRequest
                        {
                            CaptchaSid = backendResult.Error.captcha_sid,
                            Url        = backendResult.Error.captcha_img
                        };

                        VKSDK.InvokeCaptchaRequest(captchaRequest,
                                                   (captchaResponse) =>
                        {
                            if (!captchaResponse.IsCancelled)
                            {
                                var parametersWithCaptcha = new Dictionary <string, string>(parametersDict);

                                parametersWithCaptcha["captcha_sid"] = captchaResponse.Request.CaptchaSid;
                                parametersWithCaptcha["captcha_key"] = captchaResponse.EnteredString;

                                DoDispatch(parametersWithCaptcha,
                                           callback,
                                           customDeserializationFunc);
                            }
                            else
                            {
                                InvokeSafely(() => callback(new VKBackendResult <T>()
                                {
                                    ResultCode = VKResultCode.CaptchaControlCancelled
                                }));
                            }
                        });
                    }
                    else if (backendResult.ResultCode == VKResultCode.ValidationRequired)
                    {
                        var validationRequest = new VKValidationRequest
                        {
                            ValidationUri = backendResult.Error.redirect_uri
                        };

                        VKSDK.InvokeValidationRequest(validationRequest,
                                                      (vr) =>
                        {
                            if (vr.IsSucceeded)
                            {
                                DoDispatch(parametersDict, callback, customDeserializationFunc);
                            }
                            else
                            {
                                InvokeSafely(() => callback(new VKBackendResult <T> {
                                    ResultCode = VKResultCode.ValidationCanceslledOrFailed
                                }));
                            }
                        });
                    }
                    else
                    {
                        InvokeSafely(() => callback(backendResult));
                    }
                }
                else
                {
                    var backendResult = new VKBackendResult <T> {
                        ResultCode = VKResultCode.CommunicationFailed
                    };
                    InvokeSafely(() => callback(backendResult));
                }
            });
        }
Пример #2
0
        internal static void InvokeValidationRequest(VKValidationRequest request, Action<VKValidationResponse> callback)
        {
            VKExecute.ExecuteOnUIThread(() =>
                {
#if SILVERLIGHT
                    VKParametersRepository.SetParameterForId("ValidationCallback", callback);
                    RootFrame.Navigate(new Uri(string.Format("/VK.WindowsPhone.SDK;component/Pages/VKLoginPage.xaml?ValidationUri={0}", HttpUtility.UrlEncode(request.ValidationUri)), UriKind.Relative));
#else
                    var loginUserControl = new VKLoginUserControl();

                    loginUserControl.ValidationUri = request.ValidationUri;
                    loginUserControl.ValidationCallback = callback;

                    loginUserControl.ShowInPopup(Windows.UI.Xaml.Window.Current.Bounds.Width,
                         Windows.UI.Xaml.Window.Current.Bounds.Height); 
#endif

                });

        }