private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
 {
     try
     {
         var uri = e.Url;
         if (uri.AbsoluteUri.StartsWith(AMicroblogAPI.Environment.RedirectUri))//如果成功授权
         {
             var authCode = uri.ToString().Split(new string[] { "?code=" }, StringSplitOptions.None)[1];
             AMicroblogAPI.Environment.AccessToken = AMicroblog.GetAccessTokenByAuthorizationCode(authCode, AMicroblogAPI.Environment.RedirectUri);
             accessToken = AMicroblogAPI.Environment.AccessToken.Token;
             try
             {
                 if (!containImage)//如果不带图片
                 {
                     AMicroblog.PostStatus(content);
                 }
                 else//如果带图片
                 {
                     var u = new UpdateStatusWithPicInfo();
                     u.Pic    = "weiboTemp.png";
                     u.Status = content;
                     AMicroblog.PostStatusWithPic(u);
                 }
                 this.Close();
                 MessageBox.Show("分享到微博成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
             }
             catch (Exception a) { MessageBox.Show("更新微博错误!原因如下:\n" + a.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); }
         }
     }
     catch (Exception a) { MessageBox.Show(a.Message, "错误"); }
 }
Example #2
0
        public bool GetAccessToken(string code, out string access_token, out string uid)
        {
            bool is_ok = false;

            access_token = string.Empty;
            uid          = string.Empty;
            try
            {
                OAuthAccessToken token = AMicroblog.GetAccessTokenByAuthorizationCode(code, SinaConfig.RedirectUri);
                is_ok        = true;
                access_token = token.Token;
                uid          = token.UserID;
            }
            catch { }
            return(is_ok);

            //access_token = string.Empty;
            //uid = string.Empty;
            //string uri = "https://api.weibo.com/oauth2/access_token";
            //List<OAuthParameter> list = new List<OAuthParameter>();
            //list.Add(new OAuthParameter("client_id", SinaConfig.ClientId));
            //list.Add(new OAuthParameter("client_secret",SinaConfig.ClientSecret));
            //list.Add(new OAuthParameter("grant_type", GrantTypeEnum.authorization_code.ToString()));
            //list.Add(new OAuthParameter("code", code));
            //list.Add(new OAuthParameter("redirect_uri", SinaConfig.RedirectUri));
            //bool is_ok = false;
            //try
            //{
            //    string result = SinaOAuthUtil.HttpPost(uri, SinaOAuthUtil.GetQueryStringFromParameters(list));

            //    Dictionary<string, object> dic = SinaOAuthUtil.ParseJson<Dictionary<string, object>>(result);
            //    access_token = dic["access_token"].ToString();
            //    uid = dic["uid"].ToString();
            //    is_ok = true;
            //}
            //catch { }
            return(is_ok);
        }