예제 #1
0
        private async Task <string> GetAuthorizeCodeAsync()
        {
            Uri uri = new Uri("https://api.weibo.com/oauth2/authorize");

            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>("redirect_uri", appInfo.RedirectUri));
            pairs.Add(new KeyValuePair <string, string>("display", Untils.GetDisplay()));

            uri = Untils.AddHeader(uri, pairs);

            WebAuthenticationResult result = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, uri, new Uri(appInfo.RedirectUri));

            string code = "";

            switch (result.ResponseStatus)
            {
            case WebAuthenticationStatus.Success:
                code = new Uri(result.ResponseData).GetQueryParameter("code");
                break;

            case WebAuthenticationStatus.UserCancel:
                throw new Exception("user cancel authorize");

            case WebAuthenticationStatus.ErrorHttp:
                throw new Exception("http connection error");

            default:
                throw new Exception("unknow error");
            }
            return(code);
        }
예제 #2
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();
            }
        }
예제 #3
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);
            }
        }