有些东西,我也狠不下心来将他们干干净净地删除掉,所以我只能尽可能地去远离他们,于是,按照惯例,我又有机会来分享一下更新Twitter、新浪微博消息的代码了。(若有想要在 Twitter 上联系我的朋友,请直接使用 DM,这样我可以收到邮件通知)
提示:如果想要查看和同步相关的文章,可以直接点击这些文章的标签“一堆微博客”,可以查看文章的列表。
Twitter 更新消息时注意加入 postBody 参数就可以叻,更多细节可以对照 ”简版 OAuthr 认证 for C#“。
- /// <summary>、
- /// OAuth 认证更新twitter消息
- /// </summary>
- /// <param name="consumer_key">应用的consumer_key</param>
- /// <param name="consumer_secret">应用的consumer_secret</param>
- /// <param name="oauth_token">应用的access_key</param>
- /// <param name="oauth_token_secret">应用的access_secret</param>
- /// <param name="message">发送的消息</param>
- /// <param name="request_path">请求的API</param>
- private void update_twitter(
- string consumer_key,
- string consumer_secret,
- string oauth_token,
- string oauth_token_secret,
- string message,
- string request_path)
- {
- string service = "推特";
- System.Net.ServicePointManager.Expect100Continue = false;
- this.BeginInvoke(new UpdateStatusDelegate(StateInfo), new object[] { "正在将消息发布到 " + service });
- string postData = "status=" + Utility.UrlEncode(message);
- byte[] bs = Encoding.UTF8.GetBytes(postData);
- Dictionary<string, string> param = new Dictionary<string, string>();
- param = OAuth.RequestParams(
- consumer_key,
- consumer_secret,
- oauth_token,
- oauth_token_secret,
- request_path,
- "POST",
- postData,
- null);
- string head_string = OAuth.Dict2Header(param);
- try
- {
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(request_path);
- request.ContentType = "application/x-www-form-urlencoded";
- request.Method = "POST";
- request.ContentLength = bs.Length;
- if (!string.IsNullOrEmpty(proxyserver))
- {
- request.Proxy = new WebProxy(proxyserver);
- ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
- }
- request.Headers.Add("Authorization", "OAuth realm=\"http://t.yunmengze.net\"," + head_string);
- using (Stream reqStream = request.GetRequestStream())
- {
- reqStream.Write(bs, 0, bs.Length);
- reqStream.Close();
- }
- }
- catch (Exception e)
- {
- ErrorMessage("将消息发布到 " + service + " 时出现意外,建议暂时取消这一服务的同步:\r\n" + e.Message, 1);
- return;
- }
- }
如果使用代理,加上一段
- public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
- {
- //直接确认,否则打不开
- return true;
- }
再是新浪微博的,其详细的规范我不太清楚,总之是在各种错误,各种失败之后尝试了将 OAuth 的参数(当然,也包括 postBody)直接 POST 出去,结果成功了。
- /// <summary>
- /// OAuth 认证更新sina消息
- /// </summary>
- /// <param name="consumer_key">应用的consumer_key</param>
- /// <param name="consumer_secret">应用的consumer_secret</param>
- /// <param name="oauth_token">应用的access_key</param>
- /// <param name="oauth_token_secret">应用的access_secret</param>
- /// <param name="message">发送的消息</param>
- /// <param name="request_path">请求的API</param>
- private void update_sina(
- string consumer_key,
- string consumer_secret,
- string oauth_token,
- string oauth_token_secret,
- string message,
- string request_path)
- {
- string service = "新浪微博";
- System.Net.ServicePointManager.Expect100Continue = false;
- this.BeginInvoke(new UpdateStatusDelegate(StateInfo), new object[] { "正在将消息发布到 " + service });
- string postData = "status=" + Utility.UrlEncode(message);
- Dictionary<string, string> param = new Dictionary<string, string>();
- param = OAuth.RequestParams(
- consumer_key,
- consumer_secret,
- oauth_token,
- oauth_token_secret,
- request_path,
- "POST",
- postData,
- null);
- postData = OAuth.Dict2Result(param);
- byte[] bs = Encoding.UTF8.GetBytes(postData);
- try
- {
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(request_path);
- request.ContentType = "application/x-www-form-urlencoded";
- request.Method = "POST";
- request.ContentLength = bs.Length;
- using (Stream requestStream = request.GetRequestStream())
- {
- requestStream.Write(bs, 0, bs.Length);
- requestStream.Close();
- }
- }
- catch (Exception e)
- {
- ErrorMessage("将消息发布到 " + service + " 时出现意外,建议暂时取消这一服务的同步:\n" + e.Message, 1);
- return;
- }
- }
在这里对 @yummy 表示抱歉,代码整理拖了很长时间。
没有评论:
发表评论