locked
AccessToken for Windows Push Notifications returns Bad Request 400

    Question

  • I am using the following code in GetAccessToken() as given in the MSDN article to get the access token to be used in windows notifications, but it returns "Bad Request 400" and it doesn't work at all!!

    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);
    }

    400 Bad Request.........
    This indicates that there is SOMETHING WRONG WITH PROTOCOL! The request is being turned down by the server. I suspect if either PACKAGE_SECURITY_IDENTIFIER or CLIENT_SECRET is wrong then it should return some other error code; something like 500. Error code 400 means request is sent but it is turned down by the server. Isn't it?

    Can anybody please help me out............

    • Edited by Kasun SW Thursday, March 21, 2013 6:52 AM
    Thursday, March 21, 2013 6:13 AM

Answers

  • Hi Ashish,

    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

    • Marked as answer by Kasun SW Friday, March 29, 2013 10:38 AM
    Friday, March 29, 2013 10:38 AM

All replies

  • Hi Kasun Shirantha Wannlarachchi,

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.
     
    Thank you for your understanding and support.


    Min Zhu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 22, 2013 2:52 AM
    Moderator
  • Hi Min Zhu,

    Thank you very much for your reply and yes i will be waiting for a response as soon as possible.
    Friday, March 22, 2013 3:46 AM
  • Hi Kasun,

    This request has been passed on to me now - I should be able to give you something useful in a day or two. Please standby.


    Ashish Sahu [MSFT]

    Monday, March 25, 2013 2:20 PM
  • Hi Kasun,

    According to this link, 400 denotes a failure in authentication. It also talks about ways to log more information about the failure. Can you please log the these headers and see if you can find something relevant in them? - X-WNS-Debug-Trace, X-WNS-DeviceConnectionStatus, X-WNS-Error-Description, X-WNS-Msg-ID, X-WNS-NotificationStatus


    Ashish Sahu [MSFT]

    Tuesday, March 26, 2013 4:47 PM
  • Hi Ashis,

    Yes i have already gone through the link you  suggested me. It is not possible to have the information about those headers because failure occurs while fetching the access token which needs to be added in the authentication header to send notification request.

    X-WNS-Debug-Trace, X-WNS-DeviceConnectionStatus, X-WNS-Error-Description, X-WNS-Msg-ID, X-WNS-NotificationStatus can be monitored in the request header if the failure occurs while sending the notification request.

    But this error is thrown while fetching the access token which occurs even prior to initializing the notification request

    Thanx Ashis :) Waiting for a reply form you......


    • Edited by Kasun SW Wednesday, March 27, 2013 5:16 AM
    Wednesday, March 27, 2013 5:11 AM
  • Hi Ashish,

    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

    • Marked as answer by Kasun SW Friday, March 29, 2013 10:38 AM
    Friday, March 29, 2013 10:38 AM
  • Great! Thanks for the update.

    Ashish Sahu [MSFT]

    Friday, March 29, 2013 7:42 PM