1

我正在为 goodreads ( http://www.goodreads.com ) 开发一个 Windows 8 应用程序。我正在关注http://code.msdn.microsoft.com/windowsapps/Web-Authentication-d0485122/view/Discussions上的 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);

            }
4

1 回答 1

0

DataToPost2您的问题与您从未对变量执行任何操作的事实有关。我假设它包含一些需要发布的重要数据,对吗?

String DataToPost2 = "...";
GoodreadsUrl += "...";

// Why construct DataToPost2 if not using it?? 
String GetResponse2 = await SendDataAsync(GoodreadsUrl);
于 2013-03-26T16:55:52.707 回答