AccessToken for Windows Push Notifications returns

2020-02-12 21:26发布

问题:

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.......

回答1:

I suspect the problem has to do with either an incorrect package identifier, and / or incorrect client secret.

From the MSDN page Push notification service request and response headers:

RESPONSE          DESCRIPTION
---------------   --------------------------
200 OK            The request was successful.
400 Bad Request   The authentication failed. 

Update - I ran the code from the question, using FAKE credentials.

Here is the RAW HTTP request:

POST https://login.live.com/accesstoken.srf HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: login.live.com
Content-Length: 88
Expect: 100-continue
Connection: Keep-Alive

grant_type=client_credentials&client_id=test&client_secret=test&scope=notify.windows.com

Here is the server's RAW response:

HTTP/1.1 400 Bad Request
Cache-Control: no-store
Content-Length: 66
Content-Type: application/json
Server: Microsoft-IIS/7.5
X-WLID-Error: 0x80045A78
PPServer: PPV: 30 H: BAYIDSLGN2A055 V: 0
Date: Thu, 21 Mar 2013 12:34:19 GMT
Connection: close

{"error":"invalid_client","error_description":"Invalid client id"}

You will note that the response is a 400. There is also some json that indicates the type of error. In my case, the error is Invalid client id. You probably want to take a look at your response - it will give you an indication of what happened.

I used Fiddler to debug the request/ response.



回答2:

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



回答3:

If you're using the new HttpClient API and you're sure you've copied and pasted the SID/secret values correct, you might be experiencing this issue because of encoding, provided you're using the FormUrlEncodedContent class as the content of your POST operation.

Contrary to the examples in the MSDN documentation, you don't want to URL encode the SID and secret values before adding them to the KeyValuePair collection. This is because encoding is implied by the FormUrlEncodedContent class, though I'm not seeing any documentation for this behavior. Hopefully this saves someone some time because I've been wrestling with this all night...