Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJacob Shumer
2,982 PointsSystem.Net.WebException: The remote server returned an error: (401) Unauthorized.
When I hit Ctrl+F5 to run, i get an exception. I am using keys copied from my Azure Account portal.
C# > Streams and Data Processing > Streaming Data on the Net > Searching for News Headlines
Code:
var webClient = new WebClient();
webClient.Headers.Add("Ocp-Apim-Subscription-Key", "<your key here>");
byte[] searchResults = webClient.DownloadData(string.Format("https://api.cognitive.microsoft.com/bing/v7.0/news/search?q={0}&mkt=en-us", playerName));
using (var stream = new MemoryStream(searchResults))
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
Jacob Shumer
2,982 PointsI found that the solution was the way i got the API keys. I got them under the incorrect category. My new keys work correctly and thanks for the help!
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Jacob Shumer ! First, I've redacted your API key so that it's not spread out into the wild Secondly, I don't see anything obviously incorrect nor that goes against the API documentation. I'm wondering if it's possible that you've exceeded the rate limit or potentially your number of free "calls" per month? Either one of those would also result in a 401.
Jacob Shumer
2,982 PointsJacob Shumer
2,982 PointsThanks for the reply! I have not used this key at all before this. The key is under a cognitive service on Azure and couldn't find another way to get an API key.