Ejemplo n.º 1
0
        private async Task Authorize(string code)
        {
            Uri uri = new Uri("https://api.weibo.com/oauth2/access_token");

            List <KeyValuePair <string, string> > pairs = new List <KeyValuePair <string, string> >();

            pairs.Add(new KeyValuePair <string, string>("client_id", appInfo.Key));
            pairs.Add(new KeyValuePair <string, string>("client_secret", appInfo.Secret));
            pairs.Add(new KeyValuePair <string, string>("grant_type", "authorization_code"));
            pairs.Add(new KeyValuePair <string, string>("code", code));
            pairs.Add(new KeyValuePair <string, string>("redirect_uri", appInfo.RedirectUri));

            HttpFormUrlEncodedContent content = new HttpFormUrlEncodedContent(pairs);

            using (HttpClient client = new HttpClient())
            {
                DateTime time = DateTime.Now;

                HttpResponseMessage response;
                try
                {
                    response = await client.PostAsync(uri, content);
                }
                catch (Exception ex)
                {
                    throw new Exception("network error", ex);
                }
                string json = await response.Content.ReadAsStringAsync();

                JObject accessToken = JsonConvert.DeserializeObject <JObject>(json);
                UserInfo.Token     = accessToken["access_token"].ToString();
                UserInfo.ExpiresAt = Untils.ToTimestamp(time) + (long)accessToken["expires_in"];
                UserInfo.Uid       = accessToken["uid"].ToString();
            }
        }
Ejemplo n.º 2
0
        private async Task <LoginInfo> Authorize(string code)
        {
            Uri uri = new Uri("https://api.weibo.com/oauth2/access_token");

            List <KeyValuePair <string, string> > pairs = new List <KeyValuePair <string, string> >();

            pairs.Add(new KeyValuePair <string, string>("client_id", appInfo.Key));
            pairs.Add(new KeyValuePair <string, string>("client_secret", appInfo.Secret));
            pairs.Add(new KeyValuePair <string, string>("grant_type", "authorization_code"));
            pairs.Add(new KeyValuePair <string, string>("code", code));
            pairs.Add(new KeyValuePair <string, string>("redirect_uri", appInfo.RedirectUri));

            HttpFormUrlEncodedContent content = new HttpFormUrlEncodedContent(pairs);

            using (HttpClient client = new HttpClient())
            {
                DateTime time = DateTime.Now;

                HttpResponseMessage response;
                try
                {
                    response = await client.PostAsync(uri, content);
                }
                catch (Exception ex)
                {
                    throw new Exception("network error", ex);
                }
                string resultJson = await response.Content.ReadAsStringAsync();

                var tokenResult = JsonConvertHelper.JsonDeserialize <TokenResult>(resultJson);
                LoginInfo.AccessToken = tokenResult.AccessToken;
                LoginInfo.ExpiresIn   = tokenResult.ExpiresIn;
                LoginInfo.ExpiresAt   = Untils.ToTimestamp(time) + tokenResult.ExpiresIn;
                LoginInfo.User        = tokenResult.Uid;

                return(LoginInfo);
            }
        }