예제 #1
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);
            }
        }
예제 #2
0
        internal static async Task <T> ReadAsJsonAsync <T>(this IHttpContent content)
        {
            string json = await content.ReadAsStringAsync();

            return(JsonConvertHelper.JsonDeserialize <T>(json));
        }