我正在尝试让 Linq to twitter 在 wp8 应用程序上工作,我查看了整个可用文档,但在尝试演示时无法弄清楚这一点(作者说它适用于 wp8)时我得到了错误。我使用 ApplicationOnlyAuthorizer,我的目的只是为了能够阅读公共推文(无法登录和发送推文)。
主页.cs
public MainPage()
{
this.InitializeComponent();
UserTweetsWidget = new UserTweetsViewModel("xxxxxxxx", 20);
this.DataContext = this;
}
TweetModel.cs
public class TweetModel
{
public string ScreenName { get; set; }
public string UserName { get; set; }
public string Image { get; set; }
public string Text { get; set; }
public string PublicationDate { get; set; }
}
UserTweetsViewModel.cs
public class UserTweetsViewModel
{
public string Label { get; set; }
public ObservableCollection<TweetModel> Tweets { get; set; }
private const string consumerKey = “xxxxxxx”;
private const string consumerSecret = “xxxxxx”;
private const string twitterAccessToken = “xxxxxxxxxxxxxxxxx”;
private const string twitterAccessTokenSecret = “xxxxxxxxxx”;
public UserTweetsViewModel(string userName, int count)
{
String _n= userName;
int _c= count;
InitializeAsync(_n, _c);
}
private async Task InitializeAsync(string userName, int count)
{
this.Label = string.Format("Tweets by @{0}", userName);
Tweets = await GetTwitterUserTimeLine(userName, count);
}
private async Task<ObservableCollection<TweetModel>> GetTwitterUserTimeLine(string userName, int count)
{
ObservableCollection<TweetModel> result = new ObservableCollection<TweetModel>();
var auth = new ApplicationOnlyAuthorizer
{
CredentialStore = new InMemoryCredentialStore
{
ConsumerKey = consumerKey,
ConsumerSecret = consumerSecret,
OAuthToken= twitterAccessToken,
OAuthTokenSecret= twitterAccessTokenSecret
}
};
await auth.AuthorizeAsync();
TwitterContext twitterCtx = new TwitterContext(auth);
var tweets = twitterCtx.Status.Where(tweet => tweet.ScreenName == userName && tweet.Type == StatusType.Home).Take(count).ToList();
foreach (var item in tweets)
{
TweetModel tweet = new TweetModel()
{
Text = item.Text,
ScreenName = item.User.Name,
UserName = "@" + item.ScreenName,
PublicationDate = Convert.ToString(item.CreatedAt),
Image = item.User.ProfileImageUrl
};
result.Add(tweet);
}
return result;
}
}
谢谢,鲍勃