2

我正在编写一个通过 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 和令牌进行查询?

4

1 回答 1

2

您应该执行以下操作:

  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);
            }
        }
    }
    
于 2012-05-10T21:44:42.813 回答