Search code examples
asp.netpush-notificationwindows-store-apps

AccessToken for Windows Push Notifications returns Bad Request 400


PLEASE HELP!! Can't figure out why this simple code given by MSDN doesn't work....

I am using the following code in GetAccessToken() as given in the this MSDN article to get the access token to be used in windows notifications, but it returns "Bad Request 400"

PACKAGE_SECURITY_IDENTIFIER, CLIENT_SECRET are the values obtained when the app was registered with the Windows Store Dashboard

string urlEncodedSid = HttpUtility.UrlEncode(PACKAGE_SECURITY_IDENTIFIER);
string urlEncodedSecret = HttpUtility.UrlEncode(CLIENT_SECRET);

string body = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com", urlEncodedSid, urlEncodedSecret);

string response;

using (WebClient client = new WebClient())
{
    client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    response = client.UploadString("https://login.live.com/accesstoken.srf", body);
}

Any help would be highly appreciated.......


Solution

  • I found the reason for the error response. In fact it is the wrong PACKAGE_SECURITY_IDENTIFIER and CLIENT_SECRET.

    DO NOT type the values. Because associated ASCII values differ. Therefore it is always better to copy and paste directly.

    You will probably will get the access token with the simple code snippet.

    Cheers