2013-05-31 42 views
3

我發出的朋友/ IDS稱,像這樣:WCF REST JSON收集空與Twitter

GET /1.1/friends/ids.json?screen_name=blablabla HTTP/1.1 

有效響應發出:

{"ids":[97500486,32947624,8884440,2022687,28741369,33978239,10312111,950922,7682036,21688137,7696145,15876098],"next_cursor":0,"next_cursor_str":"0","previous_cursor":0,"previous_cursor_str":"0"} 

我的界面看起來是這樣的:

[OperationContract(Name = "ids.json")] 
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate="ids.json?user_id={userId}&screen_name={screenName}")] 
FriendsIdsResponse Ids(long? userId, string screenName); 

[DataContract] 
public class FriendsIdsResponse 
{ 
    public FriendsIdsResponse() 
    { 
    } 

    [DataMember(Name = "ids")] 
    public long[] Ids { get; set; } 
    [DataMember(Name = "previous_cursor")] 
    public int PreviousCursor { get; set; } 
    [DataMember(Name = "next_cursor")] 
    public int NextCursor { get; set; } 
} 

無論什麼類型的Ids是(long [],IList,List等),它總是返回null。如果我將它實例化爲ctor中的空列表,它的Count == 0。

UPDATE WCF CONFIGS,如要求:

<system.serviceModel> 
    <client> 
     <endpoint contract="TA.Twitter.Service.IFriends, TA.Twitter.Interfaces" 
          address="https://api.twitter.com/1.1/friends/" 
          binding="webHttpBinding" bindingConfiguration="WebHttpBinding" 
          ></endpoint> 
    </client> 
    <bindings> 
     <webHttpBinding> 
      <binding name="WebHttpBinding" allowCookies="true"> 
       <security mode="Transport"> 
       </security> 
      </binding> 
     </webHttpBinding> 
    </bindings> 
</system.serviceModel> 
+0

能否請您提供您所使用調用Twitter的API和反序列化響應代碼。你的合同和界面很好,所以它必須是客戶端代碼。 –

+0

@TommyGrovnes但我沒有使用任何自定義代碼,它都是WCF。從本質上講,我創建了一個ChannelFactory ,然後創建了一個.CreateChannel()(這裏是IFriends)。 –

+0

那麼你的wcf配置如何,綁定等? –

回答

1

不是一個完整的答案,但你只限於使用WCF來調用API高音? 我是WCF的忠實粉絲,但對於Twitter來說,已經有很多.net Librairies。我知道WCF存在挑戰,但我更喜歡每次都不要重新發明輪子。

Tweetsharp似乎是用於Twitter API訪問的流行的.net庫。

假設您已經有OAuth令牌,那麼使用少於10行的代碼就可以獲得朋友。

var service = new TweetSharp.TwitterService("consumerKey", "consumerSecret"); 
    service.AuthenticateWith("accessToken", "accessTokenSecret"); 

    var friends = service.ListFriendIdsOf(new TweetSharp.ListFriendIdsOfOptions() { ScreenName = "blabla" }); 
    foreach (var friend in friends) 
    { 
     Console.WriteLine("new friend :{0} ", friend); 
    } 

不到10行,不到10分鐘,似乎是一個很好的妥協

+0

我知道.NET的Twitter庫,但希望得到WCF的工作。似乎是簡單的JSON響應,應該是一件容易的事。 –

+0

肯定是的,但是你需要爲每個你想使用的Tweeter API創建操作和數據合同,並且有人已經爲你做了這個。 – Cybermaxs