我正在爲goodreads開發Windows 8應用程序(http://www.goodreads.com)。我正在關注網絡身份驗證代理示例,網址爲http://code.msdn.microsoft.com/windowsapps/Web-Authentication-d0485122/view/Discussions。我設法成功地獲得了oauth標記和oauth標記。在Windows 8應用程序中從Goodreads獲取訪問令牌
但我無法將其發送回去,並從goodreads獲取訪問令牌。我試圖使用獲取的oauth標記和令牌sectret發送構建的URL,如下面的代碼中所述。我試圖使用我從Goodreads API使用OAuth收到的oauth令牌獲取訪問令牌。
在給定的代碼中,「GetResponse2」變量總是從異步數據發送方法設置爲null。我認爲這是因爲構建了URL,但我無法弄清楚URL中哪裏出了問題。希望可以有人幫幫我。
if (oauth_token != null)
{
GoodreadsUrl = "https://www.goodreads.com/oauth/authorize?oauth_token=" + oauth_token;
System.Uri StartUri = new Uri(GoodreadsUrl);
System.Uri EndUri = new Uri(GoodreadsCallbackUrl);
WebAuthenticationResult WebAuthenticationResult =
await WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
StartUri,EndUri);
var response = WebAuthenticationResult.ResponseData;
if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
{
TimeSpan SinceEpoch2 = DateTime.UtcNow - new DateTime(1970, 1, 1);
Random Rand2 = new Random();
GoodreadsUrl = "http://www.goodreads.com/oauth/access_token";
Int32 Nonce2 = Rand2.Next(1000000000);
//
// Compute base signature string and sign it.
// This is a common operation that is required for all requests even after the token is obtained.
// Parameters need to be sorted in alphabetical order
// Keys and values should be URL Encoded.
//
String SigBaseStringParams2 = "oauth_callback=" + Uri.EscapeDataString(GoodreadsCallbackUrl);
SigBaseStringParams2 += "&" + "oauth_consumer_key=" + GoodreadsClientId;
SigBaseStringParams2 += "&" + "oauth_token=" + oauth_token;
SigBaseStringParams2 += "&" + "oauth_verifier=" + oauth_token_secret;
SigBaseStringParams2 += "&" + "oauth_nonce=" + Nonce2.ToString();
SigBaseStringParams2 += "&" + "oauth_signature_method=HMAC-SHA1";
SigBaseStringParams2 += "&" + "oauth_timestamp=" + Math.Round(SinceEpoch2.TotalSeconds);
SigBaseStringParams2 += "&" + "oauth_version=1.0";
String SigBaseString2 = "GET&";
SigBaseString2 += Uri.EscapeDataString(GoodreadsUrl) + "&" + Uri.EscapeDataString(SigBaseStringParams2);
IBuffer KeyMaterial2 = CryptographicBuffer.ConvertStringToBinary(GoodreadsClientSecret + "&", BinaryStringEncoding.Utf8);
MacAlgorithmProvider HmacSha1Provider2 = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
CryptographicKey MacKey2 = HmacSha1Provider2.CreateKey(KeyMaterial2);
IBuffer DataToBeSigned2 = CryptographicBuffer.ConvertStringToBinary(SigBaseString2, BinaryStringEncoding.Utf8);
IBuffer SignatureBuffer2 = CryptographicEngine.Sign(MacKey2, DataToBeSigned2);
String Signature2 = CryptographicBuffer.EncodeToBase64String(SignatureBuffer2);
String DataToPost2 = "OAuth oauth_callback=\"" + Uri.EscapeDataString(GoodreadsCallbackUrl) + "\", oauth_consumer_key=\"" + GoodreadsClientId + "\", oauth_nonce=\"" + Nonce2.ToString() + "\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"" + Math.Round(SinceEpoch2.TotalSeconds) + "\", oauth_version=\"1.0\", oauth_signature=\"" + Uri.EscapeDataString(Signature2) + "\"";
GoodreadsUrl += "?" + SigBaseStringParams2 + "&oauth_signature=" + Uri.EscapeDataString(Signature2);
String GetResponse2 = await SendDataAsync(GoodreadsUrl);
// DebugPrint("Received Data: " + GetResponse);
}
請不要只問我們爲你解決問題。告訴我們你是如何試圖自己解決問題的,然後向我們展示結果是什麼,並告訴我們爲什麼你覺得它不起作用。請參閱「[您嘗試過什麼?](http://whathaveyoutried.com/)」,以獲得一篇您最近需要閱讀的優秀文章。 – 2013-03-26 16:25:25
謝謝約翰,我馬上就會更新這個問題。 – har 2013-03-26 16:29:51