C#腾讯微博授权后如何获取验证码
/// <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 { <SPAN style="COLOR: #ff0000"> string response = request.Get();</SPAN> 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.GetAccessToken("https:-//open.t.qq-.-com/cgi-bin/access_token", verifier, out name); } /// <summary> /// 同步获取授权的Access Token,调用此方法时必须设置Token与TokenSecret属性的值为request_token与request_secret /// 如果获取成功, 对象实例的Token与TokenSecret属性值将改为access_token与access_secret /// </summary> /// <param name="requestUrl">API请求地址</param> /// <param name="verifier">请求授权request token时返回的验证码</param> /// <param name="name">微博帐户名</param> /// <returns>是否获取Access Token成功</returns> public bool GetAccessToken(string requestUrl, string verifier, out string name) { NameValueCollection responseData; Parameters parameters = new Parameters(); parameters.Add("oauth_token", this.Token); parameters.Add("oauth_verifier", verifier); var r = this.GetOAuthToken(requestUrl, parameters, out responseData); name = responseData == null ? null : responseData["name"]; return r; } /// ///之后操作API没有问题
里的 verifier(请求授权request token时返回的验证码)无法获得 即如何把用户程序内输入的账号密码请求后返回verifier验证码 腾讯微博的API/SDK没有提供 暂时没有研究出来
新浪微博的是在Response Body里以<oauth_verifier>(.+?)</oauth_verifier>格式返回的。
自己用正则截取一下
var requester = new HttpGet(APIUri.Authorize);
requester.Params.Add(Constants.OAuthToken, requestToken);
requester.Params.Add("userId", UrlEncode(userName));requester.Params.Add("passwd", UrlEncode(password));requester.Params.Add(Constants.OAuthCallback, "xml");
以上新浪微博没有问题