同步消息到Twitter、新浪微博

来源:http://isouth.org/archives/302.html

有些东西,我也狠不下心来将他们干干净净地删除掉,所以我只能尽可能地去远离他们,于是,按照惯例,我又有机会来分享一下更新Twitter、新浪微博消息的代码了。(若有想要在 Twitter 上联系我的朋友,请直接使用 DM,这样我可以收到邮件通知)

提示:如果想要查看和同步相关的文章,可以直接点击这些文章的标签“”,可以查看文章的列表。

Twitter 更新消息时注意加入 postBody 参数就可以叻,更多细节可以对照 ”简版 OAuthr 认证 for C#“。

  1. /// <summary>、
  2. /// OAuth 认证更新twitter消息
  3. /// </summary>
  4. /// <param name="consumer_key">应用的consumer_key</param>
  5. /// <param name="consumer_secret">应用的consumer_secret</param>
  6. /// <param name="oauth_token">应用的access_key</param>
  7. /// <param name="oauth_token_secret">应用的access_secret</param>
  8. /// <param name="message">发送的消息</param>
  9. /// <param name="request_path">请求的API</param>
  10. private void update_twitter(
  11.      string consumer_key,
  12.      string consumer_secret,
  13.      string oauth_token,
  14.      string oauth_token_secret,
  15.      string message,
  16.      string request_path)
  17. {
  18.      string service = "推特";
  19.      System.Net.ServicePointManager.Expect100Continue = false;
  20.      this.BeginInvoke(new UpdateStatusDelegate(StateInfo), new object[] { "正在将消息发布到 " + service });
  21.      string postData = "status=" + Utility.UrlEncode(message);
  22.      byte[] bs = Encoding.UTF8.GetBytes(postData);
  23.      Dictionary<string, string> param = new Dictionary<string, string>();
  24.      param = OAuth.RequestParams(
  25.          consumer_key,
  26.          consumer_secret,
  27.          oauth_token,
  28.          oauth_token_secret,
  29.          request_path,
  30.          "POST",
  31.          postData,
  32.          null);
  33.      string head_string = OAuth.Dict2Header(param);
  34.      try
  35.      {
  36.          HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(request_path);
  37.          request.ContentType = "application/x-www-form-urlencoded";
  38.          request.Method = "POST";
  39.          request.ContentLength = bs.Length;
  40.          if (!string.IsNullOrEmpty(proxyserver))
  41.          {
  42.              request.Proxy = new WebProxy(proxyserver);
  43.              ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
  44.          }
  45.          request.Headers.Add("Authorization", "OAuth realm=\"http://t.yunmengze.net\"," + head_string);
  46.          using (Stream reqStream = request.GetRequestStream())
  47.          {
  48.              reqStream.Write(bs, 0, bs.Length);
  49.              reqStream.Close();
  50.          }
  51.      }
  52.      catch (Exception e)
  53.      {
  54.          ErrorMessage("将消息发布到 " + service + " 时出现意外,建议暂时取消这一服务的同步:\r\n" + e.Message, 1);
  55.          return;
  56.      }
  57. }

如果使用代理,加上一段

  1. public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  2. {
  3.     //直接确认,否则打不开
  4.     return true;
  5. }

再是新浪微博的,其详细的规范我不太清楚,总之是在各种错误,各种失败之后尝试了将 OAuth 的参数(当然,也包括 postBody)直接 POST 出去,结果成功了。

  1. /// <summary>
  2. /// OAuth 认证更新sina消息
  3. /// </summary>
  4. /// <param name="consumer_key">应用的consumer_key</param>
  5. /// <param name="consumer_secret">应用的consumer_secret</param>
  6. /// <param name="oauth_token">应用的access_key</param>
  7. /// <param name="oauth_token_secret">应用的access_secret</param>
  8. /// <param name="message">发送的消息</param>
  9. /// <param name="request_path">请求的API</param>
  10. private void update_sina(
  11.     string consumer_key,
  12.     string consumer_secret,
  13.     string oauth_token,
  14.     string oauth_token_secret,
  15.     string message,
  16.     string request_path)
  17. {
  18.     string service = "新浪微博";
  19.     System.Net.ServicePointManager.Expect100Continue = false;
  20.     this.BeginInvoke(new UpdateStatusDelegate(StateInfo), new object[] { "正在将消息发布到 " + service });
  21.     string postData = "status=" + Utility.UrlEncode(message);
  22.     Dictionary<string, string> param = new Dictionary<string, string>();
  23.     param = OAuth.RequestParams(
  24.         consumer_key,
  25.         consumer_secret,
  26.         oauth_token,
  27.         oauth_token_secret,
  28.         request_path,
  29.         "POST",
  30.         postData,
  31.         null);
  32.     postData = OAuth.Dict2Result(param);
  33.     byte[] bs = Encoding.UTF8.GetBytes(postData);
  34.     try
  35.     {
  36.         HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(request_path);
  37.         request.ContentType = "application/x-www-form-urlencoded";
  38.         request.Method = "POST";
  39.         request.ContentLength = bs.Length;
  40.         using (Stream requestStream = request.GetRequestStream())
  41.         {
  42.             requestStream.Write(bs, 0, bs.Length);
  43.             requestStream.Close();
  44.         }
  45.     }
  46.     catch (Exception e)
  47.     {
  48.         ErrorMessage("将消息发布到 " + service + " 时出现意外,建议暂时取消这一服务的同步:\n" + e.Message, 1);
  49.         return;
  50.     }
  51. }

在这里对 @yummy 表示抱歉,代码整理拖了很长时间。 :)


没有评论: