Exemplo n.º 1
0
 public void OAuth(ResOAuth oauth)
 {
     if (OnOAuth != null)
     {
         Application.Current.Dispatcher.BeginInvoke(OnOAuth, new object[] { oauth });
     }
 }
Exemplo n.º 2
0
        public void SyncRequestOAuth(BasePacket packet, Action <ResOAuth> callback)
        {
            if (packet == null)
            {
                return;
            }
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(packet.url);
                req.ContentType = "application/x-www-form-urlencoded;encoding=utf-8";
                req.Method      = packet.method;
                req.Headers.Add("Authorization", OAuth.GetInstence().GetHeader(packet));

                using (WebResponse response = req.GetResponse())
                    using (Stream stream = response.GetResponseStream())
                        using (StreamReader streamRead = new StreamReader(stream))
                        {
                            ResOAuth oauth          = new ResOAuth();
                            string   responseString = streamRead.ReadToEnd();
                            oauth.tokenStr  = Regex.Match(responseString, @"oauth_token=([^&]+)").Groups[1].Value;           //json으로 오는 게 아니라 이렇게 해야함
                            oauth.secretStr = Regex.Match(responseString, @"oauth_token_secret=([^&]+)").Groups[1].Value;
                            bool isCallBack = false;
                            bool.TryParse(Regex.Match(responseString, @"oauth_callback_confirmed=([^&]+)").Groups[1].Value, out isCallBack);
                            oauth.isCallBack = isCallBack;
                            callback?.Invoke(oauth);
                        }
            }
            catch (WebException e)
            {
                if (e.Message.IndexOf("401") > -1)
                {
                    if (OnOAuthError != null)
                    {
                        Application.Current.Dispatcher.BeginInvoke(OnOAuthError);
                    }
                }
            }
            catch (Exception e)
            {
            }
        }