【问题标题】:Simple queries with Twitter API using only an access_token仅使用 access_token 的 Twitter API 简单查询
【发布时间】:2012-05-19 10:45:29
【问题描述】:

我正在编写一个通过 oauth 连接到 Facebook 和 Twitter 的 C#.Net WPF 4.0 应用程序。使用 Facebook Graph API,我能够授权、使用 oauth 登录、将临时 access_token 交换为几乎持久的访问令牌,然后仅通过在我的查询旁边添加 access_token 或在墙上发布来获取任何数据,像这样:[http://Url/query/access_token],所有这一切都没有任何 SDK 或任何其他库。

我尝试对 Twitter 做同样的事情,但我都搞混了。我一直在寻找有关如何像在 Facebook 中一样获取一些 Json 数据的示例,但我什么也没找到,可能是因为我不知道要搜索什么。为了能够仅使用直接 url 和令牌进行查询,我需要遵循什么流程?

【问题讨论】:

    标签: c# .net facebook-graph-api twitter


    【解决方案1】:

    您应该执行以下操作:

    1. 获取用户的访问令牌:https://dev.twitter.com/docs/auth/obtaining-access-tokens

    2. 使用以下 REST API 之一:https://dev.twitter.com/docs/api

    3. 生成 OAuth 标头并将其插入到您的请求中。下面是我的应用程序中的代码,它将推文和图像上传到推特 - 但 GET 请求将是相似的。注意:我正在使用来自 https://cropperplugins.svn.codeplex.com/svn/Cropper.Plugins/TwitPic/OAuth.cs 的 3rd-party OAuth 类

      var oauth = new OAuth.Manager();
      oauth["consumer_key"] = Settings.TWITTER_CONSUMER_KEY;
      oauth["consumer_secret"] = Settings.TWITTER_CONSUMER_SECRET;
      oauth["token"] = item.AccessToken;
      oauth["token_secret"] = item.AccessSecret;
      
      var url = "https://upload.twitter.com/1/statuses/update_with_media.xml";
      var authzHeader = oauth.GenerateAuthzHeader(url, "POST");
      
      foreach (var imageName in item.Images.Split('|'))
      {
          var fileData = PhotoThubmnailBO.GetThumbnailForImage(imageName, ThumbnailType.FullSize).Photo;
      
          // this code comes from http://cheesoexamples.codeplex.com/wikipage?title=TweetIt&referringTitle=Home
          // also see http://stackoverflow.com/questions/7442743/how-does-one-upload-a-photo-to-twitter-with-the-api-function-post-statuses-updat
          var request = (HttpWebRequest) WebRequest.Create(url);
      
          request.Method = "POST";
          request.PreAuthenticate = true;
          request.AllowWriteStreamBuffering = true;
          request.Headers.Add("Authorization", authzHeader);
      
          string boundary = "~~~~~~" +
                            Guid.NewGuid().ToString().Substring(18).Replace("-", "") +
                            "~~~~~~";
      
          var separator = "--" + boundary;
          var footer = "\r\n" + separator + "--\r\n";
          string shortFileName = imageName;
          string fileContentType = GetMimeType(shortFileName);
          string fileHeader = string.Format("Content-Disposition: file; " +
                                            "name=\"media\"; filename=\"{0}\"",
                                            shortFileName);
          var encoding = Encoding.GetEncoding("iso-8859-1");
      
          var contents = new StringBuilder();
          contents.AppendLine(separator);
          contents.AppendLine("Content-Disposition: form-data; name=\"status\"");
          contents.AppendLine();
          contents.AppendLine(item.UserMessage);
          contents.AppendLine(separator);
          contents.AppendLine(fileHeader);
          contents.AppendLine(string.Format("Content-Type: {0}", fileContentType));
          contents.AppendLine();
      
          // actually send the request
          request.ServicePoint.Expect100Continue = false;
          request.ContentType = "multipart/form-data; boundary=" + boundary;
      
          using (var s = request.GetRequestStream())
          {
              byte[] bytes = encoding.GetBytes(contents.ToString());
              s.Write(bytes, 0, bytes.Length);
              bytes = fileData;
              s.Write(bytes, 0, bytes.Length);
              bytes = encoding.GetBytes(footer);
              s.Write(bytes, 0, bytes.Length);
          }
      
          using (var response = (HttpWebResponse) request.GetResponse())
          {
              if (response.StatusCode != HttpStatusCode.OK)
              {
                  throw new Exception(response.StatusDescription);
              }
          }
      }
      

    【讨论】:

    • 好的。我想我需要那个外部 oauh 库来生成 Authz 标头?
    猜你喜欢
    • 2012-06-07
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2012-03-08
    • 2017-01-24
    • 2012-07-30
    • 2012-12-25
    相关资源
    最近更新 更多