几天来,我一直在尝试让 OAuth 在我的 Windows Phone 应用程序中与 Twitter 一起使用,但我发现的所有信息都已过时或难以理解。当我找到这篇博客文章http://samjarawan.blogspot.co.uk/2010/09/building-real-windows-phone-7-twitter_18.html时,我最终到达了某个地方,这让我一路获得了访问令牌,此时它失败了。
我的代码与博文中的代码几乎相同,几乎只是更改了消费者密钥和消费者密码。甚至他们的应用程序也不起作用。它很好地显示了 Twitter 登录屏幕,并成功通过了身份验证,但在 RequestAccessToken 函数中,此时它失败了:
if (String.IsNullOrEmpty(twitteruser.AccessToken) || String.IsNullOrEmpty(twitteruser.AccessTokenSecret))
{
Dispatcher.BeginInvoke(() => MessageBox.Show(response.Content));
return;
}
真正烦人的是消息框只显示 Unicode 替换字符 (�),没有其他内容。我还检查了 response.StatusCode 并且没问题,所以据我所知没有错误。
如果有人可以帮助我解决这个问题,那就太好了。我看过其他需要用户输入 PIN 的教程,但我也无法让其中任何一个工作。
编辑:我刚刚尝试让 TweetSharp 工作,但它再次无法获得访问令牌。这是我用于 TweetSharp 的代码:
public partial class TwitterAuthorisationPage : PhoneApplicationPage
{
private const string consumerKey = "myKey";
private const string consumerSecret = "mySecret"; // These are the correct values for my app
private const string requestTokenUri = "https://api.twitter.com/oauth/request_token";
private const string oAuthVersion = "1.0a";
private const string authorizeUri = "https://api.twitter.com/oauth/authorize";
private const string accessTokenUri = "https://api.twitter.com/oauth/access_token";
private const string callbackUri = "http://bing.com";
private TwitterService twitterService = new TwitterService(consumerKey, consumerSecret);
private OAuthRequestToken _requestToken = null;
public TwitterAuthorisationPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
twitterService.GetRequestToken((requestToken, response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
_requestToken = requestToken;
Dispatcher.BeginInvoke(() => BrowserControl.Navigate(twitterService.GetAuthorizationUri(requestToken)));
}
else
{
Dispatcher.BeginInvoke(() => MessageBox.Show("Failed to connect to Twitter. Please try again.\n" + response.StatusDescription));
}
});
}
private void ConfirmButton_Click(object sender, RoutedEventArgs e)
{
twitterService.GetAccessToken(_requestToken, PINEntry.Text, (accessToken, response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
//These lines just print ?
System.Diagnostics.Debug.WriteLine(accessToken.Token);
System.Diagnostics.Debug.WriteLine(accessToken.TokenSecret);
twitterService.AuthenticateWith(accessToken.Token, accessToken.TokenSecret);
twitterService.VerifyCredentials((user, verifyResponse) =>
{
if (verifyResponse.StatusCode == HttpStatusCode.OK)
{
Dispatcher.BeginInvoke(() => MessageBox.Show(user.Name));
}
else
{
// Fails here
Dispatcher.BeginInvoke(() => MessageBox.Show("Failed to connect to Twitter. Please try again.1\n" + verifyResponse.StatusDescription));
}
});
}
else
{
Dispatcher.BeginInvoke(() => MessageBox.Show("Failed to connect to Twitter. Please try again.0\n" + response.StatusDescription));
}
});
}
}
编辑2:可能与此有关吗?https://dev.twitter.com/blog/ssl-upgrade-for-twitterapi