使用Twitter,WCF REST JSON集合为null

时间:2013-05-31 10:37:10

标签: json wcf rest serialization twitter

我正在发出好友/ ids电话:

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中的空List,则它具有Count == 0。

更新根据要求配置WCF:

<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>

1 个答案:

答案 0 :(得分:1)

不是一个完整的答案,但您是否仅限于使用WCF来调用Tweeter API? 我是WCF的忠实粉丝,但对于Twitter,已有很多.net Librairies。我知道WCF存在挑战,但我不想每次都重新发明轮子。

Tweetsharp似乎是一个流行的.net库,用于访问Twitter API。

假设您已经拥有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分钟,似乎是一个很好的妥协