我在Windows 8 / c#中遇到了HttpClient的烦人问题。我正在做一些测试,试图从非官方的谷歌阅读器API中获取数据。我使用WebAuthenticationBroker获得OAuth,这很有效,但是当我尝试访问https://www.google.com/reader/api/0/user-info?access_token=ACCESS_TOKEN处的用户信息API时,我收到了401未经授权的消息。奇怪的是,我把我正在输入HttpClient.GetAsync()的确切网址放到我的浏览器中,它运行得很好。我也试过了头的身份验证方法,它也失败了。
string accessCode = //WebAuthenticationBroker Stuff
var req = new HttpClient();
Uri toRequest = new Uri("https://www.google.com/reader/api/0/user-info?access_token="
+ accessCode);
HttpResponseMessage results = await req.GetAsync(toRequest);
//Old Header Stuff
//req.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessCode);
//req.DefaultRequestHeaders.Add("Host", "googleapis.com");
//Debug Stuff
dlg = new MessageDialog(results.ToString());
await dlg.ShowAsync();
if (results.IsSuccessStatusCode)
{
dlg = new MessageDialog(await results.Content.ReadAsStringAsync());
}
else
{
dlg = new MessageDialog("ERROR: "+results.StatusCode.ToString());
}
await dlg.ShowAsync();