C# 在Windows 8应用程序中从Goodreads获取访问令牌
我正在为goodreads开发一个Windows8应用程序。下面是web身份验证代理示例。我成功地获得了oauth令牌和oauth令牌密钥 但我无法将其发回并从goodreads获得访问令牌。我正试图使用获得的oauth令牌和令牌sectret发送构造的URL,如下面代码中所述。我正在尝试使用oauth从Goodreads API收到的oauth令牌获取访问令牌 在给定的代码中,GetResponse2变量总是从异步数据发送方法设置为null。我认为这是因为URL的构造,但我无法找出URL中哪里出错了。希望有人能帮助我C# 在Windows 8应用程序中从Goodreads获取访问令牌,c#,windows-8,oauth-2.0,winrt-async,C#,Windows 8,Oauth 2.0,Winrt Async,我正在为goodreads开发一个Windows8应用程序。下面是web身份验证代理示例。我成功地获得了oauth令牌和oauth令牌密钥 但我无法将其发回并从goodreads获得访问令牌。我正试图使用获得的oauth令牌和令牌sectret发送构造的URL,如下面代码中所述。我正在尝试使用oauth从Goodreads API收到的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);
请不要只是要求我们为您解决问题。向我们展示你是如何试图自己解决问题的,然后向我们展示结果是什么,并告诉我们为什么你觉得它不起作用。请参阅一篇您确实需要阅读的优秀文章。谢谢John,我将很快更新此问题。您好,如果您遵循web authentication broker示例,他们也没有使用他们构建的dataToPost变量。我只是遵循同样的方法构建了它。老实说,我不知道他们为什么构造它。@Har-如果我试着调试这个,我肯定会研究这个变量以及它是如何使用的。