【问题标题】:"Your API key is not valid on this domain" when calling Disqus from WP7从 WP7 调用 Disqus 时“您的 API 密钥在此域上无效”
【发布时间】:2011-08-27 19:12:57
【问题描述】:

我正在尝试使用以下 url 访问 REST Disqus API:

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

当我在 Chrome 中访问 url 时,它工作正常。当我尝试在 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

【问题讨论】:

  • 在浏览器中放同一个url可以吗?
  • 您是否检查过变量queryUri 包含您期望的内容?当您在 Internet Explorer 中托管时会发生什么?你用 Fiddler 详细检查过 http 会话吗?
  • @alfonso 是的,它在浏览器中运行良好
  • NotFound 不会是实际错误。由于某种原因,WP7 http api 不会轻易报告真正的错误。尝试使用 fiddler 来观察流量,或者使用 Hammock 库来提出您的请求并取回真正的错误信息。
  • 确实添加到 Chris 的评论 NotFound 不是实际错误。我把你的代码放在一个空的项目里通过Fiddler验证结果,发现实际返回的结果是“{”code”: 5, “response”: “Invalid API key”}”(不填任何key时的预期)。手机仍然显示相同的 NotFound 异常。请尝试this 获取更多信息,我们或许可以为您提供帮助

标签: silverlight windows-phone-7 rest webclient disqus


【解决方案1】:

修复:

在您的 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 为您做这件事),然后您就可以开始了。

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

/ck

【讨论】:

    【解决方案2】:

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

    另一个有趣的指向项目是像http://disqussharp.codeplex.com/ 这样的库,它大部分时间都在处理身份验证。

    祝你好运!

    【讨论】:

    • DisqusSharp 没有为 WP7 启用。
    • 另外,我从 Chrome Incognito 发出了成功的请求,所以我认为这与登录无关。
    • 是的,我尝试了我的公共和秘密 API 密钥并验证它们是正确的
    • 你可以尝试为 wp7 编译 DisqusSharp。将所有文件添加到一个新的 wp7 项目中,看看它是否有效。
    • 我将响应标头更新为来自 Chrome Incognito,并且它们不包括 X-User。我认为这与问题无关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2015-11-07
    • 1970-01-01
    相关资源
    最近更新 更多