2012-05-10 18 views
2

oauth経由でFacebookとTwitterに接続するC#.Net WPF 4.0アプリケーションをコーディングしています。 Facebook Graph APIを使用すると、oauthを使用してサインインしたり、一時的なaccess_tokenをほぼ永続的なアクセストークンに交換したり、クエリの横にaccess_tokenを追加したり、壁に掲示したりするだけでデータを取得できます、[http:// Url/query/access_token]のように、SDKやその他のライブラリを一切使用しないでください。access_tokenのみを使用したTwitter APIによる簡単なクエリ

私はTwitterで同じことを試みましたが、私はすべて混乱しています。私は、Facebookでのやり方と同じように、Jsonのデータを取得する方法の例を探していましたが、何も見つかりませんでした。直接のURLとトークンだけでクエリを実行できるようにするために必要なフローは何ですか?

答えて

2

次の操作を行う必要があります。

  1. ユーザーのアクセストークンを取得します:https://dev.twitter.com/docs/auth/obtaining-access-tokens

  2. 利用REST APIを1:https://dev.twitter.com/docs/api

  3. はOAuthのヘッダを生成しに挿入あなたの要求。以下は私のアプリからのツイートや画像をtwitterにアップロードするコードですが、GETリクエストは似ています。注:私はhttps://cropperplugins.svn.codeplex.com/svn/Cropper.Plugins/TwitPic/OAuth.cs

    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); 
         } 
        } 
    } 
    
+0

オクラホマからサードパーティのOAuthクラスを使用しています。 Authzヘッダーを生成するには、外部のoauhライブラリが必要ですか? –

+0

はい、あなたはそれを必要とします。 – avs099

関連する問題