private byte[] CreateUploadData(Stream imageStream, string fileName, Dictionary <string, string> parameters, string boundary) { bool oAuth = parameters.ContainsKey("oauth_consumer_key"); string[] keys = new string[parameters.Keys.Count]; parameters.Keys.CopyTo(keys, 0); Array.Sort(keys); StringBuilder hashStringBuilder = new StringBuilder(sharedSecret, 2 * 1024); StringBuilder contentStringBuilder = new StringBuilder(); foreach (string key in keys) { #if !SILVERLIGHT // Silverlight < 5 doesn't support modification of the Authorization header, so all data must be sent in post body. if (key.StartsWith("oauth")) { continue; } #endif hashStringBuilder.Append(key); hashStringBuilder.Append(parameters[key]); contentStringBuilder.Append("--" + boundary + "\r\n"); contentStringBuilder.Append("Content-Disposition: form-data; name=\"" + key + "\"\r\n"); contentStringBuilder.Append("\r\n"); contentStringBuilder.Append(parameters[key] + "\r\n"); } if (!oAuth) { contentStringBuilder.Append("--" + boundary + "\r\n"); contentStringBuilder.Append("Content-Disposition: form-data; name=\"api_sig\"\r\n"); contentStringBuilder.Append("\r\n"); contentStringBuilder.Append(UtilityMethods.MD5Hash(hashStringBuilder.ToString()) + "\r\n"); } // Photo contentStringBuilder.Append("--" + boundary + "\r\n"); contentStringBuilder.Append("Content-Disposition: form-data; name=\"photo\"; filename=\"" + Path.GetFileName(fileName) + "\"\r\n"); contentStringBuilder.Append("Content-Type: image/jpeg\r\n"); contentStringBuilder.Append("\r\n"); UTF8Encoding encoding = new UTF8Encoding(); byte[] postContents = encoding.GetBytes(contentStringBuilder.ToString()); byte[] photoContents = ConvertNonSeekableStreamToByteArray(imageStream); byte[] postFooter = encoding.GetBytes("\r\n--" + boundary + "--\r\n"); byte[] dataBuffer = new byte[postContents.Length + photoContents.Length + postFooter.Length]; Buffer.BlockCopy(postContents, 0, dataBuffer, 0, postContents.Length); Buffer.BlockCopy(photoContents, 0, dataBuffer, postContents.Length, photoContents.Length); Buffer.BlockCopy(postFooter, 0, dataBuffer, postContents.Length + photoContents.Length, postFooter.Length); return(dataBuffer); }
private StreamCollection CreateUploadData(Stream imageStream, string fileName, Dictionary <string, string> parameters, string boundary) { var oAuth = parameters.ContainsKey("oauth_consumer_key"); var keys = new string[parameters.Keys.Count]; parameters.Keys.CopyTo(keys, 0); Array.Sort(keys); var hashStringBuilder = new StringBuilder(sharedSecret, 2 * 1024); var ms1 = new MemoryStream(); var contentStringBuilder = new StreamWriter(ms1, new UTF8Encoding(false)); foreach (var key in keys) { #if !SILVERLIGHT // Silverlight < 5 doesn't support modification of the Authorization header, so all data must be sent in post body. if (key.StartsWith("oauth", StringComparison.Ordinal)) { continue; } #endif hashStringBuilder.Append(key); hashStringBuilder.Append(parameters[key]); contentStringBuilder.Write("--" + boundary + "\r\n"); contentStringBuilder.Write("Content-Disposition: form-data; name=\"" + key + "\"\r\n"); contentStringBuilder.Write("\r\n"); contentStringBuilder.Write(parameters[key] + "\r\n"); } if (!oAuth) { contentStringBuilder.Write("--" + boundary + "\r\n"); contentStringBuilder.Write("Content-Disposition: form-data; name=\"api_sig\"\r\n"); contentStringBuilder.Write("\r\n"); contentStringBuilder.Write(UtilityMethods.MD5Hash(hashStringBuilder.ToString()) + "\r\n"); } // Photo contentStringBuilder.Write("--" + boundary + "\r\n"); contentStringBuilder.Write("Content-Disposition: form-data; name=\"photo\"; filename=\"" + Path.GetFileName(fileName) + "\"\r\n"); contentStringBuilder.Write("Content-Type: image/jpeg\r\n"); contentStringBuilder.Write("\r\n"); contentStringBuilder.Flush(); var photoContents = ConvertNonSeekableStreamToByteArray(imageStream); var ms2 = new MemoryStream(); var postFooterWriter = new StreamWriter(ms2, new UTF8Encoding(false)); postFooterWriter.Write("\r\n--" + boundary + "--\r\n"); postFooterWriter.Flush(); var collection = new StreamCollection(new[] { ms1, photoContents, ms2 }); return(collection); }
private string CalculateAuthSignature(Dictionary <string, string> parameters) { var sorted = parameters.OrderBy(p => p.Key); StringBuilder sb = new StringBuilder(ApiSecret); foreach (KeyValuePair <string, string> pair in sorted) { sb.Append(pair.Key); sb.Append(pair.Value); } string signature = UtilityMethods.MD5Hash(sb.ToString()); return(signature); }
public string AuthCalcUrl(string frob, AuthLevel authLevel) { if (sharedSecret == null) { throw new SignatureRequiredException(); } string hash = sharedSecret + "api_key" + apiKey + "frob" + frob + "perms" + UtilityMethods.AuthLevelToString(authLevel); hash = UtilityMethods.MD5Hash(hash); string url = AuthUrl + "?api_key=" + apiKey + "&perms=" + UtilityMethods.AuthLevelToString(authLevel) + "&frob=" + frob; url += "&api_sig=" + hash; return(url); }
private string CalculateAuthSignature(Dictionary <string, string> parameters) { #if !SILVERLIGHT var sorted = new SortedList <string, string>(); foreach (var pair in parameters) { sorted.Add(pair.Key, pair.Value); } #else var sorted = parameters.OrderBy(p => p.Key); #endif var sb = new StringBuilder(ApiSecret); foreach (var pair in sorted) { sb.Append(pair.Key); sb.Append(pair.Value); } return(UtilityMethods.MD5Hash(sb.ToString())); }
/// <summary> /// Calculates the URL to redirect the user to Flickr web site for /// authentication. Used by Web applications. /// See <see cref="AuthGetFrob"/> for example code. /// </summary> /// <param name="authLevel">The <see cref="AuthLevel"/> stating the maximum authentication level your application requires.</param> /// <param name="extra">An extra string value which Flickr will return to the callback URL along with the frob.</param> /// <returns>The url to redirect the user to.</returns> public string AuthCalcWebUrl(AuthLevel authLevel, string extra) { CheckApiKey(); CheckSigned(); string hash = sharedSecret + "api_key" + apiKey + "perms" + UtilityMethods.AuthLevelToString(authLevel); string url = AuthUrl + "?api_key=" + apiKey + "&perms=" + UtilityMethods.AuthLevelToString(authLevel); if (!String.IsNullOrEmpty(extra)) { hash += "extra" + extra; url += "&extra=" + Uri.EscapeDataString(extra); } hash = UtilityMethods.MD5Hash(hash); url += "&api_sig=" + hash; return(url); }