用于Windows推送通知的AccessToken返回错误请求400

问题描述 投票:3回答:3

请帮忙!!无法弄清楚为什么MSDN给出的这个简单代码不起作用....

我在this MSDN article中使用GetAccessToken()中的以下代码来获取在Windows通知中使用的访问令牌,但它返回“Bad Request 400”

PACKAGE_SECURITY_IDENTIFIER,CLIENT_SECRET是在Windows应用商店信息中心注册应用时获得的值

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

任何帮助将受到高度赞赏.......

asp.net push-notification windows-store-apps
3个回答
3
投票

我怀疑问题与不正确的包标识符和/或不正确的客户端密钥有关。

从MSDN页面Push notification service request and response headers

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

更新 - 我使用FAKE凭据运行问题中的代码。

这是RAW HTTP请求:

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

这是服务器的RAW响应:

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"}

您将注意到响应是400.还有一些json指示错误的类型。就我而言,错误是Invalid client id。您可能想看看您的回复 - 它会告诉您发生了什么。

我使用Fiddler来调试请求/响应。


2
投票

我找到了错误响应的原因。实际上它是错误的PACKAGE_SECURITY_IDENTIFIER和CLIENT_SECRET。

请勿键入值。因为关联的ASCII值不同。因此,直接复制和粘贴总是更好。

您可能会使用简单的代码段获取访问令牌。

干杯


0
投票

如果您正在使用新的HttpClient API,并且您确定已经正确复制并粘贴了SID /机密值,则可能因为编码而遇到此问题,前提是您使用FormUrlEncodedContent类作为您的内容POST操作。

MSDN documentation中的示例相反,在将它们添加到KeyValuePair集合之前,您不希望对SID和机密值进行URL编码。这是因为编码是由FormUrlEncodedContent类隐含的,尽管我没有看到任何documentation这种行为。希望这可以节省一些时间,因为我整晚都在努力摔跤......

© www.soinside.com 2019 - 2024. All rights reserved.