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 ヘッダーを生成し、リクエストに挿入します。以下は、ツイートと画像を twitter にアップロードする私のアプリのコードですが、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);
            }
        }
    }
    
于 2012-05-10T21:44:42.813 に答える