日期:2014-05-18  浏览次数:21034 次

【WPF】—腾讯微博授权后如何获取验证码 高分求教@
C# code

 /// <summary>
        /// 使用默认的API地址同步获取未授权的Request Token. 
        /// 如果获取成功, 对象实例的Token与TokenSecret属性值将改为request_token与request_secret
        /// </summary>
        /// <param name="callback">返回地址</param>
        /// <returns>是否获取request token成功</returns>
        public bool GetRequestToken(string callback)
        {
            return this.GetRequestToken("https://open.t.qq.com/cgi-bin/request_token", callback);
        }

        /// <summary>
        /// 同步获取未授权的Request Token. 
        /// 如果获取成功, 对象实例的Token与TokenSecret属性值将改为request_token与request_secret
        /// </summary>
        /// <param name="requestUrl">API请求地址</param>
        /// <param name="callback">返回地址</param>
        /// <returns>是否获取request token成功</returns>
        public bool GetRequestToken(string requestUrl, string callback)
        {
            this.Token = string.Empty;
            this.TokenSecret = string.Empty;

            NameValueCollection responseData;
            Parameters parameters = new Parameters();
            parameters.Add("oauth_callback", string.IsNullOrEmpty(callback) ? "null" : callback);
            var r = this.GetOAuthToken(requestUrl, parameters, out responseData);

            return r;
        }

/// <summary>
        /// 获取授权Token
        /// </summary>
        /// <param name="requestUrl">API请求地址</param>
        /// <param name="parameters"></param>
        /// <param name="responseData"></param>
        /// <returns></returns>
        private bool GetOAuthToken(string requestUrl, Parameters parameters, out NameValueCollection responseData)
        {
            this.LastError = null;
            responseData = null;
            parameters.Add("oauth_consumer_key", this.AppKey);
            parameters.Add("oauth_signature_method", "HMAC-SHA1");
            parameters.Add("oauth_timestamp", Util.GenerateTimestamp());
            parameters.Add("oauth_nonce", Util.GenerateRndNonce());
            parameters.Add("oauth_version", "1.0");
            parameters.Add("oauth_signature", this.GenerateSignature("GET", requestUrl, parameters));
            string url = string.Concat(requestUrl, "?", parameters.BuildQueryString(true));

            SyncHttpRequest request = new SyncHttpRequest(url, this.Charset);
            try
            {
               [color=#FF0000] string response = request.Get();[/color]
                if (!string.IsNullOrEmpty(response))
                {
                    responseData = HttpUtility.ParseQueryString(response, this.Charset);
                    this.Token = responseData["oauth_token"];
                    this.TokenSecret = responseData["oauth_token_secret"];
                    return !string.IsNullOrEmpty(this.Token) && !string.IsNullOrEmpty(this.TokenSecret);
                }
            }
            catch (Exception ex)
            {
                this.LastError = ex;
            }
            return false;
        }

/// <summary>
        /// 使用默认的API地址同步获取授权的Access Token,调用此方法时必须设置Token与TokenSecret属性的值为request_token与request_secret
        /// 如果获取成功, 对象实例的Token与TokenSecret属性值将改为access_token与access_secret
        /// </summary>
        /// <param name="verifier">请求授权request token时返回的验证码</param>
        /// <param name="name">微博帐户名</param>
        /// <returns>是否获取Access Token成功</returns>
        public bool GetAccessToken(string verifier, out string name)
        {
            return this