“您的 API 密钥在此域上无效”从 WP7 调用 Disqus 时

发布于 2024-12-01 12:03:21 字数 2423 浏览 0 评论 0原文

我尝试使用以下网址访问 REST Disqus API:

http://disqus.com/api/3.0/threads/listPosts.json
?api_key=myKey
&forum=myForum
&thread:ident=myIdent

当我在 Chrome 中访问该网址时,它工作正常。当我尝试在 WebClient 中下载它时,我遇到困难:

            WebClient data = new WebClient();
            Uri queryUri = new Uri(DisqusQuery + ident, UriKind.Absolute);
            data.DownloadStringCompleted += new DownloadStringCompletedEventHandler(onDownloadCompleted);
            data.DownloadStringAsync(queryUri);

DownloadStringCompletedEventArgs 包含以下错误:

{"The remote server returned an error: NotFound."}
 at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__1(Object sendState)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethoThe thread '<No Name>' (0xfc10086) has exited with code 0 (0x0).

我可能做错了什么?

更新:在Fiddler中查看显示响应是这样的:

HTTP/1.1 400 BAD REQUEST
Date: Sun, 28 Aug 2011 14:51:39 GMT
Server: Apache/2.2.14 (Ubuntu)
Vary: Cookie,Accept-Encoding
p3p: CP="DSP IDC CUR ADM DELi STP NAV COM UNI INT PHY DEM"
Content-Length: 68
Connection: close
Content-Type: application/json
X-Pad: avoid browser bug

{"code": 11, "response": "Your API key is not valid on this domain"}

这是当请求来自Chrome Incognito(未登录到disqus)时的响应:

HTTP/1.1 200 OK
Date: Mon, 29 Aug 2011 17:00:29 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Ratelimit-Remaining: 1000
Content-Encoding: gzip
Vary: Cookie,Accept-Encoding
X-Ratelimit-Limit: 1000
p3p: CP="DSP IDC CUR ADM DELi STP NAV COM UNI INT PHY DEM"
X-Ratelimit-Reset: 1314640800
Content-Length: 3120
Connection: close
Content-Type: application/json

/* expected JSON response */

更新2:以上错误是使用我的公钥。使用密钥会导致:

HTTP/1.1 403 FORBIDDEN
Date: Sun, 28 Aug 2011 20:40:32 GMT
Server: Apache/2.2.14 (Ubuntu)
Vary: Cookie,Accept-Encoding
p3p: CP="DSP IDC CUR ADM DELi STP NAV COM UNI INT PHY DEM"
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json

2a
{"code": 5, "response": "Invalid API key"}
0

I'm trying to access the REST Disqus API using the following url:

http://disqus.com/api/3.0/threads/listPosts.json
?api_key=myKey
&forum=myForum
&thread:ident=myIdent

When I go to the url in Chrome, it works fine. When I try to download it in WebClient, I have difficulty:

            WebClient data = new WebClient();
            Uri queryUri = new Uri(DisqusQuery + ident, UriKind.Absolute);
            data.DownloadStringCompleted += new DownloadStringCompletedEventHandler(onDownloadCompleted);
            data.DownloadStringAsync(queryUri);

The DownloadStringCompletedEventArgs contain the following error:

{"The remote server returned an error: NotFound."}
 at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__1(Object sendState)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethoThe thread '<No Name>' (0xfc10086) has exited with code 0 (0x0).

What could I be doing wrong?

Update: Looking in Fiddler shows that the response is this:

HTTP/1.1 400 BAD REQUEST
Date: Sun, 28 Aug 2011 14:51:39 GMT
Server: Apache/2.2.14 (Ubuntu)
Vary: Cookie,Accept-Encoding
p3p: CP="DSP IDC CUR ADM DELi STP NAV COM UNI INT PHY DEM"
Content-Length: 68
Connection: close
Content-Type: application/json
X-Pad: avoid browser bug

{"code": 11, "response": "Your API key is not valid on this domain"}

Here is the response when the request is from Chrome Incognito (not logged in to disqus):

HTTP/1.1 200 OK
Date: Mon, 29 Aug 2011 17:00:29 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Ratelimit-Remaining: 1000
Content-Encoding: gzip
Vary: Cookie,Accept-Encoding
X-Ratelimit-Limit: 1000
p3p: CP="DSP IDC CUR ADM DELi STP NAV COM UNI INT PHY DEM"
X-Ratelimit-Reset: 1314640800
Content-Length: 3120
Connection: close
Content-Type: application/json

/* expected JSON response */

Update 2: The above error is using my public key. Using the secret key results in:

HTTP/1.1 403 FORBIDDEN
Date: Sun, 28 Aug 2011 20:40:32 GMT
Server: Apache/2.2.14 (Ubuntu)
Vary: Cookie,Accept-Encoding
p3p: CP="DSP IDC CUR ADM DELi STP NAV COM UNI INT PHY DEM"
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json

2a
{"code": 5, "response": "Invalid API key"}
0

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

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

发布评论

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

评论(2

入画浅相思 2024-12-08 12:03:22

看起来浏览器正在获取其他信息,例如用户名或其他信息:X-User:anon:182210122933。当 WebClient 收到响应时,此信息丢失。我想这与您在浏览器中登录或者您的 api 密钥中有拼写错误有关。

对您来说另一个有趣的点项目是像 http://disqussharp.codeplex.com/ 这样的库,它最多处理身份验证的时间。

祝你好运!

Looks like the browser is getting additional info like username or something: X-User: anon:182210122933. This is missing when WebClient gets its response back. I guess this has something todo with the fact that you are logged in in the browser or that you have a typo in your api key.

Another interesting pointroject for you would be a library like http://disqussharp.codeplex.com/ which handles authentication most of the time.

Good luck!

月隐月明月朦胧 2024-12-08 12:03:21

修复:

将类似于以下行的内容添加到您的 HttpRequest:

client.Headers[HttpRequestHeader.Referer] = "http://mywebsite.com";

更长的描述:

问题与 Windows Phone 设置 HTTP Referer 标头的方式有关。

当从浏览器地址栏运行成功的请求时,Fiddler 向我展示了以下内容:

GET /api/3.0/forums/listPosts.json?forum=disqus&api_key=jRml... HTTP/1.1
Accept: */*
Accept-Language: en-US
Accept-Encoding: gzip, deflate, peerdist
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7; InfoPath.3; MS-RTC LM 8)
Connection: Keep-Alive
Host: disqus.com
Cookie: disqus_unique=...
X-P2P-PeerDist: Version=1.0

当我在 Fiddler 中检查 Silverlight 发送的请求时,我看到以下内容:

GET /api/3.0/forums/listPosts.json?forum=disqus&api_key=jRml... HTTP/1.1
Accept: */*
Referer: file:///Applications/Install/9036AAF3-F213-4CFB-B57E-576A05E1896D/Install/
Accept-Encoding: identity
User-Agent: NativeHost
Host: disqus.com
Connection: Keep-Alive

通过删除 Referer 标头并通过 Fiddler 重新提交,查询按我的预期工作!所以...您需要做的就是手动将 HTTP Referer 标头设置为您可以控制的内容(而不是让 Silverlight 为您完成),然后就可以开始了。

哦 - 还要确保您使用的是公钥密钥,而不是秘密密钥。

/ck

FIX:

Add something similar to the following line to your HttpRequest:

client.Headers[HttpRequestHeader.Referer] = "http://mywebsite.com";

Longer Description:

The problem has to do with the way Windows Phone is setting the HTTP Referer header.

When running the successful request from the browser address bar, Fiddler showed me this:

GET /api/3.0/forums/listPosts.json?forum=disqus&api_key=jRml... HTTP/1.1
Accept: */*
Accept-Language: en-US
Accept-Encoding: gzip, deflate, peerdist
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7; InfoPath.3; MS-RTC LM 8)
Connection: Keep-Alive
Host: disqus.com
Cookie: disqus_unique=...
X-P2P-PeerDist: Version=1.0

When I examined the request sent by Silverlight in Fiddler, I saw the following:

GET /api/3.0/forums/listPosts.json?forum=disqus&api_key=jRml... HTTP/1.1
Accept: */*
Referer: file:///Applications/Install/9036AAF3-F213-4CFB-B57E-576A05E1896D/Install/
Accept-Encoding: identity
User-Agent: NativeHost
Host: disqus.com
Connection: Keep-Alive

By removing the Referer header and resubmitting via Fiddler, the query worked as I expected! So... all you need to do is manually set the HTTP Referer header to something you control (rather than letting Silverlight do it for you) and you should be good to go.

Oh - and also make sure you're using your public key, not the secret key.

/ck

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