2011-08-27 44 views
0

我嘗試使用以下URL訪問REST API Disqus調用Disqus時:「您的API密鑰是不會在這個域中是有效的」,從WP7

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). 

什麼可能我是做錯了什麼?

更新:尋找在提琴手錶明響應是這樣的:

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無痕(未登錄到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 
+0

如果您在瀏覽器中放置相同的url,它工作嗎? – alf

+0

您是否檢查過變量queryUri包含您期望的內容?當您在Internet Explorer中託管時會發生什麼?您是否使用過Fiddler來詳細檢查http對話? – AnthonyWJones

+0

@alfonso是的,它在瀏覽器中正常工作 –

回答

1

FIX:

添加類似於以下行的東西到你的HttpRequest:

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

較長的描述:

問題與順便做Windows Phone的是設置HTTP Referer頭。

當運行在瀏覽器地址欄的請求成功,提琴手向我展示了這一點:

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 

當我檢查了的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

0

貌似瀏覽器越來越像用戶名或什麼其他信息:X-用戶:匿名:182210122933。當WebClient收到迴應時,這是缺少的。我想這與您在瀏覽器中登錄或您的API密鑰中存在拼寫錯誤的事實不符。

另一個有趣的指標是你的大部分時間處理驗證的庫,如http://disqussharp.codeplex.com/

祝你好運!

+0

DisqusSharp未啓用WP7。 –

+0

另外,我向Chrome Incognito發出了一個成功的請求,所以我不認爲它與登錄有關。 –

+0

您是否已經仔細檢查了您的api密鑰以查看它是否正確? – JLaanstra

相關問題