在Windows 8应用程序中从Goodreads获取访问令牌
我正在为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);
}
您的问题与您从未对DataToPost2
变量执行任何操作有关。我假设它包含一些需要发布的重要数据,对吗?
String DataToPost2 = "...";
GoodreadsUrl += "...";
// Why construct DataToPost2 if not using it??
String GetResponse2 = await SendDataAsync(GoodreadsUrl);
嗨,如果你遵循Web身份验证代理示例,他们也不会使用它们构造的dataToPost变量。我只是遵循相同的方法,并构建了这个方法。老实说,我不知道,他们为什么要这样做。 – har 2013-03-26 17:55:26
@哈尔 - 如果我试图调试这个,我肯定会研究这个变量以及它是如何使用的。 – 2013-03-26 18:47:28
请不要只问我们为你解决问题。告诉我们你是如何试图自己解决问题的,然后向我们展示结果是什么,并告诉我们为什么你觉得它不起作用。请参阅“[您尝试过什么?](http://whathaveyoutried.com/)”,以获得一篇您最近需要阅读的优秀文章。 – 2013-03-26 16:25:25
谢谢约翰,我马上就会更新这个问题。 – har 2013-03-26 16:29:51