当前位置: 首页 > 编程语言 > C#

FlickrAPI请求签名无效(控制台模拟)share

时间:2023-04-11 02:04:14 C#

FlickrAPI请求签名无效(控制台模拟)为了我自己。然后,我将使用我将了解的有关FlickAPI的新功能扩展此应用程序。所以这只是我想玩的东西。但是现在我在获取请求令牌时遇到了一些问题。我在这里遵循了Flickr身份验证文档:Flickr身份验证我还找到了这个Mathlabscript:FlickrAPI和基于OAuth的用户身份验证因此,基于这些来源,我现在有以下控制台应用程序:私有静态字符串ConsumerKey="1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a";staticvoidMain(string[]args){Randomrand=newRandom();stringnonce=rand.Next(9999999).ToString();字符串时间戳=((int)(DateTime.UtcNow-newDateTime(1970,1,1)).TotalSeconds).ToString();Console.WriteLine("随机数:"+随机数);Console.WriteLine("时间戳:"+时间戳);安慰。WriteLine("消费者密钥:"+ConsumerKey);Console.WriteLine("AppSecret:"+Secret);//请求urlStringBuilderb=newStringBuilder();b.Append("http://www.flickr.com/services/oauth/request_token");b.追加("?");b.Append("oauth_nonce=");b.追加(随机数);b.Append("&oauth_timestamp=");b.追加(时间戳);b.Append("&oauth_consumer_key=");b.追加(消费者密钥);b.Append("&oauth_callback=oob");b.Append("&oauth_signature_method=HMAC-SHA1");字符串requesturl=b.ToString();Console.WriteLine("RequestUrl:"+requesturl);//基本url字符串basestring;StringBuilderbs=newStringBuilder();bs.Append("GET&");bs.Append(UrlHelper.Encode("http://www.flickr.com/services/oauth/request_token")+"&");basestring=bs.ToString();StringBuilderp=newStringBuilder();p.Append("oauth_callback=oob");p.Append("&oauth_consumer_key=");p.Append(ConsumerKey);p.Append("oauth_nonce=");p.Append(nonce);p.Append("&oauth_signature_method=HMAC-SHA1");p.Append("&oauth_timestamp=");p.Append(时间戳);字符串参数=UrlHelper.Encode(p.ToString());basestring+=参数;Console.WriteLine("基础字符串:"+基础字符串);System.Text.ASCIIEncoding编码=newSystem.Text.ASCIIEncoding();字符串键=秘密+“&”;Console.WriteLine("密钥:"+密钥);byte[]keyByte=encoding.GetBytes(键);//--创建消息加密byte[]messageBytes=encoding.GetBytes(basestring);//--使用hmac-sha1和提供的密钥HMACSHA1加密消息hmacsha1=newHMACSHA1(keyByte);byte[]hashmessage=hmacsha1.ComputeHash(messageBytes);//--签名字符串signature=ByteToString(hashmessage);Console.WriteLine("签名:"+signature);Console.WriteLine("最终请求:"+requesturl+"&oauth_signature="+signature);控制台.ReadKey(true);}publicstaticstringByteToString(byte[]buff){stringsbinary="";对于(inti=0;i%3D!!!(不是%3d)//HtmlUtility.UrlEncode创建小写编码的标签!//这里我使用了IanHopkins的urlencode类stringencodedUrl=UrlHelper.Encode(ApiEndpoint);stringencodedParameters=UrlHelper.Encode(参数);//生成basestring字符串basestring=httpMethod+"&"+encodedUrl+"&";参数=UrlHelper.Encode(参数);basestring=basestring+参数;//hmac-sha1加密:System.Text.ASCIIEncodingencoding=newSystem.Text.ASCIIEncoding();//创建密钥(request_token可以为空字符串)stringkey=Secret+"&"+request_token;byte[]keyByte=encoding.GetBytes(key);//创建要加密的消息byte[]messageBytes=encoding.GetBytes(basestring);//使用hmac-sha1和提供的密钥HMACSHA1加密消息hmacsha1=newHMACSHA1(keyByte);byte[]hashmessage=hmacsha1.ComputeHash(messageBytes);//signature是生成的hmac-sha1哈希字符串的base64格式signature=System.Convert.ToBase64String(hashmessage);//对签名进行编码以使其成为url安全的并返回编码后的urlreturnUrlHelper.Encode(signature);}//unix纪元时间的生成器publicstaticStringGetTimestamp(){intepoch=(int)(DateTime.UtcNow-newDateTime(1970,1,1)).TotalSeconds;返回纪元.ToString();}}伊恩合kins的UrlHelper类用于url编码//////URL编码类。注意:使用风险自负。///作者:IanHopkins(http://www.lucidhelix.com)///日期:2008年12月23日///(由t3rse(http://www.t3rse.com)移植到C#)///publicclassUrlHelper{publicstaticstringEncode(stringstr){varcharClass=String.Format("0-9a-zA-Z{0}",Regex.Escape("-_.!~*'()"));返回Regex.Replace(str,String.Format("[^{0}]",charClass),newMatchEvaluator(EncodeEvaluator));}publicstaticstringEncodeEvaluator(Matchmatch){return(match.Value=="")?"+":String.Format("%{0:X2}",Convert.ToInt32(match.Value[0]));}publicstaticstringDecodeEvaluator(Matchmatch){returnConvert.ToChar(int.Parse(match.Value.Substring(1),System.Globalization.NumberStyles.HexNumber)).ToString();}publicstaticstringDecode(stringstr){returnRegex.Replace(str.Replace('+',''),"%[0-9a-zA-Z][0-9a-zA-Z]",new匹配评估器(解码评估器));你是从头开始写这个吗?如果是这样,你不应该请改用http://flickrnet.codeplex.com/。这个库已经为你完成了繁重的工作。以上就是C#学习教程的全部内容:InvalidsignatureusedtosignFlickrAPIrequests(simulatedintheconsole)。网络收藏不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: