2012-05-10 47 views
2

我正在編寫一個通過oauth連接到Facebook和Twitter的C#.Net WPF 4.0應用程序。藉助Facebook Graph API,我可以授權,使用oauth進行登錄,將臨時access_token交換爲幾乎持久的訪問令牌,然後僅通過在查詢旁邊添加access_token來獲取任何數據,或在牆上發佈,像這樣:[http:// Url/query/access_token],以及所有這些沒有任何SDK或任何其他庫。使用Twitter API的簡單查詢只使用access_token

我試圖用Twitter做同樣的事情,但我都混在一起。我一直在尋找如何在Facebook上獲取一些Json數據的例子,但是我什麼也沒找到,可能是因爲我不知道要搜索什麼。我需要遵循的流程是什麼,才能僅通過直接url和令牌進行查詢?

回答

2

,你應該做到以下幾點:

  1. 獲得訪問令牌的用戶:https://dev.twitter.com/docs/auth/obtaining-access-tokens

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

  3. 產生OAuth頭並將其插入到你的申請。下面是我的應用程序的代碼,它將twitter和圖片上傳到twitter中 - 但GET請求會類似。注:我使用的是第三方的OAuth類從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

確定。我想我需要那個外部的oauh庫來生成一個Authz頭文件? –

+0

是的,你確實需要它。 – avs099