private void ProcessOAuthResponse(IAsyncResult callbackResult, SDK.OAuthRequestType type)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
                using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
                {
                    string result = httpWebStreamReader.ReadToEnd();
                    int tokenPosition = result.IndexOf(ParameterNameAccessToken, System.StringComparison.OrdinalIgnoreCase);
                    if(tokenPosition != 0)
                    {
                        const string tokenNameValueSeparator = "\":\"";
                        const char tokenStopSymbol = '\"';
                        StringBuilder builder = new StringBuilder();
                        tokenPosition += SDK.ParameterNameAccessToken.Length + tokenNameValueSeparator.Length;
                        while (tokenPosition < result.Length && !result[tokenPosition].Equals(tokenStopSymbol))
                        {
                            builder.Append(result[tokenPosition]);
                            tokenPosition++;
                        }
                        this._accessToken = builder.ToString();
                        AuthCallbackStruct callbackStruct = this._updateCallback;
                        if (type == SDK.OAuthRequestType.OAuthTypeAuth)
                        {
                            builder.Clear();
                            tokenPosition = result.IndexOf(SDK.ParameterNameRefreshToken, System.StringComparison.OrdinalIgnoreCase) + SDK.ParameterNameRefreshToken.Length + tokenNameValueSeparator.Length;
                            while (tokenPosition < result.Length && !result[tokenPosition].Equals(tokenStopSymbol))
                            {
                                builder.Append(result[tokenPosition]);
                                tokenPosition++;
                            }
                            this._refreshToken = builder.ToString();

                            callbackStruct = this._authCallback;
                        }
                        if (callbackStruct.SaveSession)
                        {
                            SaveSession();
                        }
                        if (callbackStruct.CallbackContext != null && callbackStruct.OnSuccess != null)
                        {
                            callbackStruct.CallbackContext.Dispatcher.BeginInvoke(() => callbackStruct.OnSuccess.Invoke());
                        }
                    }
                    else
                    {
                        ProcessOAuthError(new Exception(ErrorNoTokenSentByServer), type);
                    }
                }
            }
            catch (Exception e)
            {
                ProcessOAuthError(e, type);
            }
        }
 private void ProcessOAuthError(Exception e, SDK.OAuthRequestType type)
 {
     if (type == SDK.OAuthRequestType.OAuthTypeAuth && this._authCallback.OnError != null && this._authCallback.CallbackContext != null)
     {
         this._authCallback.CallbackContext.Dispatcher.BeginInvoke(() => this._authCallback.OnError.Invoke(e));
     }
     else if (type == SDK.OAuthRequestType.OAuthTypeUpdateToken && this._updateCallback.OnError != null)
     {
         this._updateCallback.CallbackContext.Dispatcher.BeginInvoke(() => this._updateCallback.OnError.Invoke(e));
     }
 }
        private void BeginGetOAuthResponse(IAsyncResult result, SDK.OAuthRequestType type)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)result.AsyncState;
                Stream postStream = request.EndGetRequestStream(result);

                string parameters = null;
                if (type == SDK.OAuthRequestType.OAuthTypeAuth)
                {
                    parameters = String.Format(DataTemplateAuthTokenRequest, new object[] {this._code, this._redirectUrl, this._appId, this._appSecretKey});
                }
                else if (type == SDK.OAuthRequestType.OAuthTypeUpdateToken)
                {
                    parameters = String.Format(DataTemplateAuthTokenUpdateRequest, this._refreshToken, this._appId, this._appSecretKey);
                }
                // ReSharper disable once AssignNullToNotNullAttribute
                // for now, it's correct to throw ArgumentNullException, if it'll be
                byte[] byteArray = Encoding.UTF8.GetBytes(parameters);

                postStream.Write(byteArray, 0, byteArray.Length);
                postStream.Close();

                request.BeginGetResponse(arg => ProcessOAuthResponse(arg, type), request);
            }
            catch (Exception e)
            {
                ProcessOAuthError(new Exception(SdkException, e), type);
            }
        }
        private void BeginOAuthRequest(SDK.OAuthRequestType type)
        {
            try
            {
                Uri myUri = new Uri(UriTokenRequest);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.BeginGetRequestStream(arg => BeginGetOAuthResponse(arg, type), request);

            }
            catch (Exception e)
            {
                ProcessOAuthError(new Exception(SdkException, e), type);
            }
        }
 public MainPage()
 {
     this.InitializeComponent();
     this._sdk = new SDK(AppId, AppPublicKey, AppSecretKey, RedirectUrl, Permissions);
 }