Ejemplo n.º 1
0
        /// <summary>
        /// Calculates the URL to redirect the user to Flickr web site for
        /// authentication. Used by desktop application.
        /// See <see cref="AuthGetFrob"/> for example code.
        /// </summary>
        /// <param name="frob">The FROB to be used for authentication.</param>
        /// <param name="authLevel">The <see cref="AuthLevel"/> stating the maximum authentication level your application requires.</param>
        /// <returns>The url to redirect the user to.</returns>
        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);
        }
Ejemplo n.º 2
0
        private StreamCollection CreateUploadData(Stream imageStream, string fileName, Dictionary <string, string> parameters, string boundary)
        {
            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)
            {
                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");
            }

            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);
        }
Ejemplo n.º 3
0
        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()));
        }
Ejemplo n.º 4
0
        /// <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 textToHash = sharedSecret + "api_key" + apiKey;
            string url        = AuthUrl + "?api_key=" + apiKey + "&perms=" + UtilityMethods.AuthLevelToString(authLevel);

            if (!string.IsNullOrEmpty(extra))
            {
                textToHash += "extra" + extra;
                url        += "&extra=" + Uri.EscapeDataString(extra);
            }

            textToHash += "perms" + UtilityMethods.AuthLevelToString(authLevel);

            string hash = UtilityMethods.MD5Hash(textToHash);

            url += "&api_sig=" + hash;

            return(url);
        }