Twitter HMACSHA1签名创建C#

时间:2018-04-09 14:13:48

标签: c# twitter twitter-oauth

我正试图通过C#& amp;我收到401 Unauthorized错误 - "无法验证您"这不是有帮助的。要尝试诊断问题,我想确保签名创建正常工作。

Twitter的签名创建文档页面为here。我编写了一个简单的单元测试,它应该根据文档中指定的值生成签名。但是,当我获得不同的签名字符串时,测试失败。文档中的值是错误的还是#t是我使用的代码?但是,我见过的所有C#Twitter代码都使用类似的哈希码。

[TestMethod]
public void TwitterSignatureCreation() {
  var signingKey = "kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw&LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE";
  var baseString = "POST&https%3A%2F%2Fapi.twitter.com%2F1.1%2Fstatuses%2Fupdate.json&include_entities%3Dtrue%26oauth_consumer_key%3Dxvz1evFS4wEEPTGEFPHBog%26oauth_nonce%3DkYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1318622958%26oauth_token%3D370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb%26oauth_version%3D1.0%26status%3DHello%2520Ladies%2520%252B%2520Gentlemen%252C%2520a%25";

  var hmac = new HMACSHA1( Encoding.ASCII.GetBytes( signingKey ) );
  var hash = hmac.ComputeHash( Encoding.ASCII.GetBytes( baseString ) );

  var signatureString = Convert.ToBase64String( hash );
  var expectedSignatureString = "hCtSmYh+iHYCEqBWrE7C7hYmtUk=";

  Assert.AreEqual( expectedSignatureString, signatureString );
}

Assert.AreEqual failed. 
Expected:<hCtSmYh+iHYCEqBWrE7C7hYmtUk=>. 
Actual:<hOhJgzPMpZtXHCTeluIS1VdHUH8=>. 

0 个答案:

没有答案