private void BrowserNavigating(object sender, NavigatingEventArgs e)
        {
            //QQ 空间授权成功后直接返回token,不需要用code换取token
            if (currentType == SocialType.QZone && e.Uri.AbsoluteUri.Contains("access_token"))
            {
                //http://qzs.qq.com/open/mobile/login/proxy.html?&t=1363316329#&openid=&appid=100394029&access_token=D1600ED336D42D024DFB5A0618A56B8C&key=270e66a425575f5130efe0d416252469&serial=&token_key=&browser=0&browser_error=0&status_os=&sdkv=&status_machine=
                AccessToken token = new AccessToken();
                token.Token = QueryStringHelper.GetQueryString(e.Uri, "access_token");
                token.OpenId = QueryStringHelper.GetQueryString(e.Uri, "openid");
                action(token);
                return;
            }

            if (!e.Uri.AbsoluteUri.Contains("code=") && !e.Uri.AbsoluteUri.Contains("code ="))
            {
                return;
            }
            var arguments = e.Uri.AbsoluteUri.Split('?');
            string code = arguments[1];
            if (currentType == SocialType.Tencent)
            {
                var sp = arguments[1].Split('&');
                code = sp[0];

                //open id
                client.Tag = sp[1].Split('=')[1];
            }

            SocialKit.GetToken(currentType, client, code, (p) =>
            {
                action(p);
            });
        }
Example #2
0
 public static void LogOff(SocialType type)
 {
     if (MessageBox.Show(LangResource.LogOffConfirm, "", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
     {
         return;
     }
     using (var store = IsolatedStorageFile.GetUserStoreForApplication())
     {
         string path = string.Format(ACCESS_TOKEN_PREFIX, type);
         if (store.FileExists(path))
         {
             store.DeleteFile(path);
         }
         if (type == SocialType.Weibo)
         {
             WeiboAccessToken = null;
         }
         else if (type == SocialType.Tencent)
         {
             TencentAccessToken = null;
         }
         else if (type == SocialType.Renren)
         {
             RenrenAccessToken = null;
         }
         else if (type == SocialType.QZone)
         {
             QZoneAccessToken = null;
         }
     }
     Deployment.Current.Dispatcher.BeginInvoke(delegate
     {
         MessageBox.Show(LangResource.LogOffSuccess);
     });
 }
        internal static void GetToken(SocialType type, ClientInfo client, string code, Action <AccessToken> action)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(GetTokenUrl(type, client, code));

            httpWebRequest.Method = "POST";

            httpWebRequest.BeginGetResponse((p) =>
            {
                HttpWebRequest request = (HttpWebRequest)p.AsyncState;
                HttpWebResponse httpWebResponse;
                try
                {
                    httpWebResponse = (HttpWebResponse)request.EndGetResponse(p);
                }
                catch (WebException ex)
                {
                    return;
                }
                if (httpWebResponse != null)
                {
                    using (var stream = httpWebResponse.GetResponseStream())
                    {
                        AccessToken token = new AccessToken();
                        if (type == SocialType.Tencent)
                        {
                            using (var reader = new System.IO.StreamReader(stream))
                            {
                                string text = reader.ReadToEnd();
                                if (!string.IsNullOrEmpty(text))
                                {
                                    //access_token=ec70e646177f025591e4282946c19b67&expires_in=604800&name=xshf12345
                                    var acc = text.Split('&');
                                    foreach (var item in acc)
                                    {
                                        var single = item.Split('=');
                                        if (single[0] == "access_token")
                                        {
                                            token.Token = single[1];
                                        }
                                        else if (single[0] == "expires_in")
                                        {
                                            token.ExpiresTime = DateTime.Now.AddSeconds(Convert.ToInt32(single[1]));
                                        }
                                        else if (single[0] == "name")
                                        {
                                            token.UserInfo = single[1];
                                        }
                                    }
                                    token.OpenId = client.Tag;
                                }
                            }
                        }
                        else if (type == SocialType.Weibo)
                        {
                            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Weibo.WeiboAccessToken));
                            var item          = ser.ReadObject(stream) as Weibo.WeiboAccessToken;
                            item.ExpiresTime  = DateTime.Now.AddSeconds(Convert.ToDouble(item.expires_in));
                            token.Token       = item.access_token;
                            token.ExpiresTime = item.ExpiresTime;
                            token.UserInfo    = item.uid;
                        }
                        else if (type == SocialType.Renren)
                        {
                            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Renren.RenrenAccessToken));
                            var item          = ser.ReadObject(stream) as Renren.RenrenAccessToken;
                            item.ExpiresTime  = DateTime.Now.AddSeconds(Convert.ToDouble(item.expires_in));
                            token.Token       = item.access_token;
                            token.ExpiresTime = item.ExpiresTime;
                            token.UserInfo    = item.user.name;
                        }
                        string filePath = string.Format(SocialAPI.ACCESS_TOKEN_PREFIX, type.ToString());
                        JsonHelper.SerializeData <AccessToken>(filePath, token);
                        action(token);
                    }
                }
            }, httpWebRequest);
        }
Example #4
0
        internal static void GetToken(SocialType type, ClientInfo client, string code, Action<AccessToken> action)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(GetTokenUrl(type, client, code));
            httpWebRequest.Method = "POST";

            httpWebRequest.BeginGetResponse((p) =>
            {
                HttpWebRequest request = (HttpWebRequest)p.AsyncState;
                HttpWebResponse httpWebResponse;
                try
                {
                    httpWebResponse = (HttpWebResponse)request.EndGetResponse(p);
                }
                catch (WebException ex)
                {
                    return;
                }
                if (httpWebResponse != null)
                {
                    using (var stream = httpWebResponse.GetResponseStream())
                    {
                        AccessToken token = new AccessToken();
                        if (type == SocialType.Tencent)
                        {
                            using (var reader = new System.IO.StreamReader(stream))
                            {
                                string text = reader.ReadToEnd();
                                if (!string.IsNullOrEmpty(text))
                                {
                                    //access_token=ec70e646177f025591e4282946c19b67&expires_in=604800&name=xshf12345
                                    var acc = text.Split('&');
                                    foreach (var item in acc)
                                    {
                                        var single = item.Split('=');
                                        if (single[0] == "access_token")
                                        {
                                            token.Token = single[1];
                                        }
                                        else if (single[0] == "expires_in")
                                        {
                                            token.ExpiresTime = DateTime.Now.AddSeconds(Convert.ToInt32(single[1]));
                                        }
                                        else if (single[0] == "name")
                                        {
                                            token.UserInfo = single[1];
                                        }
                                    }
                                    token.OpenId = client.Tag;
                                }
                            }
                        }
                        else if (type == SocialType.Weibo)
                        {
                            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Weibo.WeiboAccessToken));
                            var item = ser.ReadObject(stream) as Weibo.WeiboAccessToken;
                            item.ExpiresTime = DateTime.Now.AddSeconds(Convert.ToDouble(item.expires_in));
                            token.Token = item.access_token;
                            token.ExpiresTime = item.ExpiresTime;
                            token.UserInfo = item.uid;
                        }
                        else if (type == SocialType.Renren)
                        {
                            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Renren.RenrenAccessToken));
                            var item = ser.ReadObject(stream) as Renren.RenrenAccessToken;
                            item.ExpiresTime = DateTime.Now.AddSeconds(Convert.ToDouble(item.expires_in));
                            token.Token = item.access_token;
                            token.ExpiresTime = item.ExpiresTime;
                            token.UserInfo = item.user.name;
                        }
                        string filePath = string.Format(SocialAPI.ACCESS_TOKEN_PREFIX, type.ToString());
                        JsonHelper.SerializeData<AccessToken>(filePath, token);
                        action(token);                        
                    }
                }
            }, httpWebRequest);
        }