仅使用access_token的 Twitter API 进行简单查询

本文关键字:API 简单 查询 Twitter access token | 更新日期: 2023-09-27 18:33:44

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

我试图在Twitter上做同样的事情,但我都搞混了。我一直在寻找如何以与在Facebook中相同的方式获取一些Json数据的示例,但我一无所获,可能是因为我不知道该搜索什么。我需要遵循哪些流程才能仅使用直接 url 和令牌进行查询?

仅使用access_token的 Twitter API 进行简单查询

您应该执行以下操作:

  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 中的第三方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);
            }
        }
    }