C2DM 授权密钥 CRLF 和 .Net WebRequest

发布于 2024-12-06 17:45:02 字数 723 浏览 0 评论 0原文

我们正在使用 C2DM 并用 C# 编写服务器应用程序。

我们使用 WebRequest 组件通过 POST 获取授权密钥。这行得通,我们会得到一个编码的授权密钥。当我们将此密钥添加到要发送消息的请求的 auth 标头时,问题就出现了。

WebRequest request = WebRequest.Create("url");
...
request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + AuthorizationKey);

由于密钥具有 LF 字符,我们会得到一个异常...

Unhandled Exception: System.ArgumentException: Specified value has invalid CRLF characters.
Parameter name: value

   at System.Net.WebHeaderCollection.CheckBadChars(String name, Boolean isHeaderValue)

   at System.Net.WebHeaderCollection.Add(String name, String value)

CheckBadChars() 似乎拒绝授权密钥,因为它认为密钥中有一个 CRLF,而实际上只有一个 LF。

有谁知道我们如何解决这个问题?

We are using C2DM and are writing the server app in C#.

We obtain the authorization key with a POST using the WebRequest component. This works and we get back an encoded authorization key. The problem comes when we add this key to the auth header of the request that is going to send the message.

WebRequest request = WebRequest.Create("url");
...
request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + AuthorizationKey);

Since the key has the LF character, we get an exception...

Unhandled Exception: System.ArgumentException: Specified value has invalid CRLF characters.
Parameter name: value

   at System.Net.WebHeaderCollection.CheckBadChars(String name, Boolean isHeaderValue)

   at System.Net.WebHeaderCollection.Add(String name, String value)

It would seem that CheckBadChars() is refusing the authorization key because it thinks there is a CRLF in the key, when in fact there is only a LF.

Does anyone have any ideas how we could get around this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2024-12-13 17:45:02

也许你只是在最后得到了一个应该删除的换行符?

编辑:我阅读了文档(位于 http: //code.google.com/apis/accounts/docs/AuthForInstalledApps.html#Response),它说您应该得到如下所示的响应:

SID=DQAAAGgA...7Zg8CTN
LSID=DQAAAGsA...lk8BBbG
Auth=DQAAAGgA...dk3fA5N

有三个换行符。因此,您需要做的是选择以 Auth= 开头的行并从那里获取授权令牌。

Maybe you’re just getting a linebreak at the end that you’re supposed to remove?

Edit: I read the documentation (at http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#Response) and it says you’re supposed to get back a response that looks like this:

SID=DQAAAGgA...7Zg8CTN
LSID=DQAAAGsA...lk8BBbG
Auth=DQAAAGgA...dk3fA5N

There’s your three linebreaks. So what you need to do is pick out the the line starting with Auth= and grab the authorization token from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文