예제 #1
0
        public async Task <List <Icon> > SearchForIcons(string term)
        {
            OAuth.OAuthRequest request = new OAuth.OAuthRequest();
            request.ConsumerKey     = _settings.Value.TheNounProject_ConsumerKey;
            request.ConsumerSecret  = _settings.Value.TheNounProject_ConsumerKeySecret;
            request.Method          = "GET";
            request.SignatureMethod = OAuth.OAuthSignatureMethod.HmacSha1;
            request.Version         = "1.0";
            request.RequestUrl      = _apiBaseUrl + term;

            string authHeader = request.GetAuthorizationHeader();

            using (var http = new HttpClient())
            {
                http.DefaultRequestHeaders.Add("Authorization", authHeader);

                var httpResponse = await http.GetAsync(request.RequestUrl);

                if (httpResponse.IsSuccessStatusCode)
                {
                    var jsonString = await httpResponse.Content.ReadAsStringAsync();

                    var result = JsonConvert.DeserializeObject <NounProjectIconSearchResult>(jsonString);
                    return(result.Icons.Select(i => i.ToIcon()).ToList());
                }
                else
                {
                    return(new List <Icon>());
                }
            }
        }
예제 #2
0
        protected override void AddAuthentication(HttpRequestMessage message, string url, string method = "GET")
        {
            var request = new OAuth.OAuthRequest
            {
                ConsumerKey     = ConsumerKey,
                ConsumerSecret  = ConsumerSecret,
                Token           = Token,
                TokenSecret     = TokenSecret,
                Type            = OAuth.OAuthRequestType.ProtectedResource,
                SignatureMethod = OAuth.OAuthSignatureMethod.HmacSha1,
                RequestUrl      = url,
                Version         = "1.0",
                Method          = method
            };
            var auth = request.GetAuthorizationHeader().Replace("OAuth ", "");

            message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("OAuth", auth);
        }
예제 #3
0
        public bool Tweet(string imageFile, string message, oAuthTwitter oAuth)
        {
            bool bupdated = false;

            try
            {
                //HttpContext.Current.Response.Write("<script>alert(\""+imageFile+"\")</script>");
                oauth.ConsumerKey    = oAuth.ConsumerKey;
                oauth.ConsumerSecret = oAuth.ConsumerKeySecret;
                oauth.TokenSecret    = oAuth.AccessTokenSecret;
                oauth.Token          = oAuth.AccessToken;
                // oauth["consumer_key"] = oAuth.ConsumerKey;

                //oauth["consumer_secret"] = oAuth.ConsumerKeySecret;
                //oauth["token"] = oAuth.AccessToken;
                //oauth["token_secret"] = oAuth.AccessTokenSecret;

                var url = GetTwitterUpdateUrl(imageFile, message);
                if (url == twitterUrl1)
                {
                    strdic.Add("status", message);
                }
                oauth.RequestUrl = url;
                oauth.Method     = "POST";
                var authzHeader = oauth.GetAuthorizationHeader();
                var request     = (HttpWebRequest)WebRequest.Create(url);

                request.Method                    = "POST";
                request.PreAuthenticate           = true;
                request.AllowWriteStreamBuffering = true;
                request.Headers.Add("Authorization", authzHeader);

                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

                if (imageFile != null)
                {
                    string boundary = "======" +
                                      Guid.NewGuid().ToString().Substring(18).Replace("-", "") +
                                      "======";

                    var separator = "--" + boundary;
                    var footer    = "\r\n" + separator + "--\r\n";

                    string shortFileName   = Path.GetFileName(imageFile);
                    string fileContentType = GetMimeType(shortFileName);
                    string fileHeader      = string.Format("Content-Disposition: file; " +
                                                           "name=\"media\"; filename=\"{0}\"",
                                                           shortFileName);
                    var encoding = System.Text.Encoding.GetEncoding("iso-8859-1");

                    var contents = new System.Text.StringBuilder();
                    contents.AppendLine(separator);
                    contents.AppendLine("Content-Disposition: form-data; name=\"status\"");
                    contents.AppendLine();
                    contents.AppendLine(message);
                    contents.AppendLine(separator);
                    contents.AppendLine(fileHeader);
                    contents.AppendLine(string.Format("Content-Type: {0}", fileContentType));
                    contents.AppendLine();

                    request.ServicePoint.Expect100Continue = false;
                    request.ContentType = "multipart/form-data; boundary=" + boundary;
                    // actually send the request
                    using (var s = request.GetRequestStream())
                    {
                        byte[] bytes = encoding.GetBytes(contents.ToString());
                        s.Write(bytes, 0, bytes.Length);
                        bytes = File.ReadAllBytes(imageFile);
                        s.Write(bytes, 0, bytes.Length);
                        bytes = encoding.GetBytes(footer);
                        s.Write(bytes, 0, bytes.Length);
                    }
                }


                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    //   HttpContext.Current.Response.Write("<script>alert(\"" + response.StatusCode + "\")</script>");

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        bupdated = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                // logger.Error(ex.Message);
                //using (StreamWriter _testData = new StreamWriter(HttpContext.Current.Server.MapPath("~/log.txt"), true))
                //{
                //    _testData.WriteLine("Error on PhotoUpload : " + ex.Message); // Write the file.

                //}
            }


            return(bupdated);
        }