0

我正在开发 windows 8 应用程序,以下是我使用 c# 和 windows 8 发布到 twitter 的代码

        const string oauthVersion = "1.0";
        const string oauthSignatureMethod = "HMAC-SHA1";

        var oauthNonce = new Random().Next(123400, 9999999);
        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
        var oauth_timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
        var baseString = string.Format(baseFormat,
                                    consumerKey,
                                    oauthNonce,
                                    oauthSignatureMethod,
                                    oauth_timestamp,
                                    oauth_token,
                                    oauthVersion,
                                    Uri.EscapeDataString(status)
                                    );

        baseString = string.Concat("POST&", Uri.EscapeDataString(resourceUrl),
                     "&", Uri.EscapeDataString(baseString));

        IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(ConsumerSecret + "&" + oauth_token_secret, BinaryStringEncoding.Utf8);

        MacAlgorithmProvider hmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
        CryptographicKey macKey = hmacSha1Provider.CreateKey(keyMaterial);
        IBuffer dataToBeSigned = CryptographicBuffer.ConvertStringToBinary(baseString, BinaryStringEncoding.Utf8);
        IBuffer signatureBuffer = CryptographicEngine.Sign(macKey, dataToBeSigned);
        String signature = CryptographicBuffer.EncodeToBase64String(signatureBuffer);

        const string headerFormat = "OAuth oauth_consumer_key=\"{0}\", oauth_nonce=\"{1}\", " +
                                    "oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", " +
                                    "oauth_timestamp=\"{4}\", oauth_token=\"{5}\", " +
                                    "oauth_version=\"{6}\"";

        var authHeader = string.Format(headerFormat,
                                Uri.EscapeDataString(consumerKey),
                                Uri.EscapeDataString(oauthNonce.ToString()),
                                Uri.EscapeDataString(signature),
                                Uri.EscapeDataString(oauthSignatureMethod),
                                Uri.EscapeDataString(oauth_timestamp),
                                Uri.EscapeDataString(oauth_token),
                                Uri.EscapeDataString(oauthVersion)
                        );

        var postBody = "status=" + Uri.EscapeDataString(status);

        var httpClient = new HttpClient();
        //httpClient.BaseAddress = new Uri(resourceUrl);
        httpClient.DefaultRequestHeaders.ExpectContinue = false;
        httpClient.DefaultRequestHeaders.Add("Authorization", authHeader);

        byte[] content = System.Text.Encoding.Unicode.GetBytes(postBody);

        var byteArrayContent = new ByteArrayContent(content);
        byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
        var response = httpClient.PostAsync(resourceUrl, byteArrayContent);
        var httpResponseMessage = response.Result;
        var isSuccessStatusCode = httpResponseMessage.IsSuccessStatusCode;

它不断给出未经授权的错误,请帮助

4

1 回答 1

-1

使用现有的开源 Twitter 库检查您的代码

推特用户

推特RT

Linq2推特

鸣叫夏普

于 2013-07-23T05:00:24.210 回答