【问题标题】:AccessToken for Windows Push Notifications returns Bad Request 400Windows 推送通知的 AccessToken 返回错误请求 400
【发布时间】:2017-12-22 18:32:24
【问题描述】:

请帮忙!!想不通为什么 MSDN 给出的这个简单代码不起作用....

我在 GetAccessToken() 中使用this MSDN article 中给出的以下代码来获取要在 Windows 通知中使用的访问令牌,但它返回“错误请求 400”

PACKAGE_SECURITY_IDENTIFIER、CLIENT_SECRET 是应用在 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);
}

任何帮助将不胜感激......

【问题讨论】:

标签: asp.net push-notification windows-store-apps


【解决方案1】:

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

来自 MSDN 页面Push notification service request and response headers

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

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

这是原始 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 来调试请求/响应。

【讨论】:

  • 400 Bad Request......... 这表明协议有问题!服务器正在拒绝该请求。我怀疑如果 PACKAGE_SECURITY_IDENTIFIER 或 CLIENT_SECRET 错误,那么它应该返回一些其他错误代码;类似于 500。错误代码 400 表示请求已发送,但被服务器拒绝。不是吗?
  • 更改 PACKAGE_SECURITY_IDENTIFIER 或 CLIENT_SECRET 后,您是否能够获取访问令牌?
  • @Kasun - 我没有亲自尝试获取访问令牌。关于 400 - 请查看我提供的链接。它说对于这个请求 400 意味着身份验证失败。
【解决方案2】:

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

不要键入值。因为关联的 ASCII 值不同。因此,最好直接复制粘贴。

您可能会通过简单的代码 sn-p 获得访问令牌。

干杯

【讨论】:

  • 我遇到了同样的问题,但是它发现400 Bad Request 的返回值有点误导,因为它表明请求中有一些语法错误,而不是身份验证问题。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 2019-07-11
    相关资源
    最近更新 更多