Error 400 Bad Request when using CloudMediaContext with Azure Media Services
When I was developing my first Azure Media Services application based on the how-to guide, I had a problem when I tried to initialize the CloudMediaContext. I was using code similar to:
string accountName = "MyAccount";
string accountKey = "abcdef12-3456-7890-abcd-ef1234567890";
CloudMediaContext mediaContext = new CloudMediaContext(accountName, accountKey);
I got the following error when initializing the CloudMediaContext in Visual Studio:
WebException was unhandled
The remote server returned an error: (400) Bad Request.
I used Fiddler, http://www.fiddler2.com/, to trace the SSL traffic between my client and the server and found that I was getting the error:
"error":"invalid_client","error_description":"ACS50012: Authentication failed.
This led me to realize that most likely the problem was with my account name or account key. I tried a couple of different combinations of IDs and keys and realized that it was my account key that was incorrect. The first parameter that CloudMediaContext needs is the account name. This is the friendly name that you set on step 10 in the Setup Guide when you ran the PowerShell script Add-MediaServicesAccount and specified the –AccountName parameter. The second parameter that CloudMediaContext needs is the account key. This is the key you got in step 12 in the Setup Guide when you ran the $accountdetails.accountkey command. Note that the incorrect account key at the beginning of this article contains a number of dashes and is actually a GUID. The correct account key should end with an equals sign.
I modified my code so that I used the correct account key similar to:
string accountKey = "AbCdEfGhIjKlMnOpQrStUvWxYz1234567890=";
With this change I was able to get update connect and continue.