Esempio n. 1
0
 protected GitHubResponse(SocialHttpResponse response)
 {
     RateLimit          = Int32.Parse(response.Headers["X-RateLimit-Limit"]);
     RateLimitRemaining = Int32.Parse(response.Headers["X-RateLimit-Remaining"]);
     RateLimitReset     = SocialUtils.GetDateTimeFromUnixTime(response.Headers["X-RateLimit-Reset"]);
     Response           = response;
 }
Esempio n. 2
0
        public SocialHttpResponse GetResponse()
        {
            // Merge the query string
            string url = new UriBuilder(Url).MergeQueryString(QueryString).ToString();

            // Initialize the request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            // Set the method
            request.Method      = Method;
            request.Credentials = Credentials;

            // Add any headers
            // TODO: Add headers

            // Add the request body (if a POST request)
            if (Method == "POST" && PostData != null && PostData.Count > 0)
            {
                string dataString = SocialUtils.NameValueCollectionToQueryString(PostData);
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = dataString.Length;
                using (Stream stream = request.GetRequestStream()) {
                    stream.Write(Encoding.UTF8.GetBytes(dataString), 0, dataString.Length);
                }
            }

            // Get the response
            try {
                return(new SocialHttpResponse((HttpWebResponse)request.GetResponse()));
            } catch (WebException ex) {
                return(new SocialHttpResponse((HttpWebResponse)ex.Response, ex));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Get the most recent media published by a user.
        /// </summary>
        /// <param name="identifier">The identifier of the user.</param>
        /// <param name="options">The search options with any optional parameters.</param>
        /// <returns>Returns the raw JSON response from the API.</returns>
        public string GetRecentMedia(string identifier, InstagramMediaSearchOptions options)
        {
            // Declare the query string
            NameValueCollection qs = new NameValueCollection {
                { "access_token", Client.AccessToken }
            };

            // Add any optional parameters
            if (options != null)
            {
                if (options.Count > 0)
                {
                    qs.Add("count", options.Count + "");
                }
                if (options.MinId != null)
                {
                    qs.Add("min_id", options.MinId + "");
                }
                if (options.MaxId != null)
                {
                    qs.Add("max_id", options.MaxId + "");
                }
            }

            // Perform the call to the API
            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://api.instagram.com/v1/users/" + identifier + "/media/recent/", qs));
        }
Esempio n. 4
0
        /// <summary>
        /// Gets the raw JSON response from the Instagram API with media from the specified <code>tag</code>.
        /// </summary>
        /// <param name="tag">The name of the tag.</param>
        /// <param name="minTagId"></param>
        /// <param name="maxTagId"></param>
        public string GetRecentMedia(string tag, string minTagId = null, string maxTagId = null)
        {
            // Declare the query string
            NameValueCollection qs = new NameValueCollection();

            if (!String.IsNullOrWhiteSpace(Client.AccessToken))
            {
                qs.Add("access_token", Client.AccessToken);
            }
            else if (!String.IsNullOrWhiteSpace(Client.ClientId))
            {
                qs.Add("client_id", Client.ClientId);
            }

            // Add any optional parameters
            if (!String.IsNullOrWhiteSpace(minTagId))
            {
                qs.Add("min_tag_id", minTagId);
            }
            if (!String.IsNullOrWhiteSpace(maxTagId))
            {
                qs.Add("max_tag_id", maxTagId);
            }

            // Perform the call to the API
            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://api.instagram.com/v1/tags/" + tag + "/media/recent/", qs));
        }
        public string GetAll(string username, VimeoChannelsSort sort, int page, int perPage)
        {
            // Initialize the query string
            NameValueCollection query = new NameValueCollection {
                { "format", "json" },
                { "method", "vimeo.channels.getAll" }
            };

            // Add optional parameters
            if (!String.IsNullOrEmpty(username))
            {
                query.Add("user_id", username);
            }
            if (sort != VimeoChannelsSort.Default)
            {
                query.Add("sort", SocialUtils.CamelCaseToUnderscore(sort));
            }
            if (page > 0)
            {
                query.Add("page", page + "");
            }
            if (perPage > 0)
            {
                query.Add("per_page", perPage + "");
            }

            // Call the Vimeo API
            return(Client.DoHttpRequestAsString("GET", "http://vimeo.com/api/rest/v2", query, null));
        }
        public SocialQueryString GetQueryString()
        {
            SocialQueryString qs = new SocialQueryString();

            if (Count > 0)
            {
                qs.Add("count", Count);
            }
            if (MaxTimestamp != null)
            {
                qs.Add("max_timestamp", SocialUtils.GetUnixTimeFromDateTime(MaxTimestamp.Value));
            }
            if (MinTimestamp != null)
            {
                qs.Add("min_timestamp", SocialUtils.GetUnixTimeFromDateTime(MinTimestamp.Value));
            }
            if (MinId != null)
            {
                qs.Add("min_id", MinId);
            }
            if (MaxId != null)
            {
                qs.Add("max_id", MaxId);
            }
            return(qs);
        }
Esempio n. 7
0
        /// <summary>
        /// Gets the feed of the specified user or page.
        /// </summary>
        /// <param name="identifier">The ID or name of the user/page.</param>
        /// <param name="options">The options for the call to the API.</param>
        /// <returns>The raw JSON response from the API.</returns>
        public string GetFeed(string identifier, FacebookFeedOptions options)
        {
            // Declare the query string
            NameValueCollection query = new NameValueCollection();

            if (!String.IsNullOrWhiteSpace(Client.AccessToken))
            {
                query.Add("access_token", Client.AccessToken);
            }
            if (options.Limit > 0)
            {
                query.Add("limit", options.Limit + "");
            }
            if (options.Since > 0)
            {
                query.Add("since", options.Since + "");
            }
            if (options.Until > 0)
            {
                query.Add("until", options.Until + "");
            }

            // Make the call to the API
            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://graph.facebook.com/" + identifier + "/feed", query));
        }
Esempio n. 8
0
        public static VimeoChannel Parse(XElement xChannel)
        {
            if (xChannel == null || xChannel.Name != "channel")
            {
                return(null);
            }
            VimeoChannel channel = new VimeoChannel(xChannel)
            {
                Id                 = SocialUtils.GetElementValue <int>(xChannel, "id"),
                Name               = SocialUtils.GetElementValue <string>(xChannel, "name"),
                Description        = SocialUtils.GetElementValue <string>(xChannel, "description"),
                Logo               = SocialUtils.GetElementValue <string>(xChannel, "logo"),
                Badge              = SocialUtils.GetElementValue <string>(xChannel, "badge"),
                Url                = SocialUtils.GetElementValue <string>(xChannel, "url"),
                Rss                = SocialUtils.GetElementValue <string>(xChannel, "rss"),
                CreatedOn          = SocialUtils.GetElementValue <DateTime>(xChannel, "created_on"),
                CreatorId          = SocialUtils.GetElementValue <int>(xChannel, "creator_id"),
                CreatorDisplayName = SocialUtils.GetElementValue <string>(xChannel, "creator_display_name"),
                CreatorUrl         = SocialUtils.GetElementValue <string>(xChannel, "creator_url"),
                IsCreator          = SocialUtils.GetElementValueOrDefault <string>(xChannel, "is_creator") == "yes",
                IsMod              = SocialUtils.GetElementValueOrDefault <string>(xChannel, "is_mod") == "yes",
                TotalVideos        = SocialUtils.GetElementValue <int>(xChannel, "total_videos"),
                TotalSubscribers   = SocialUtils.GetElementValue <int>(xChannel, "total_subscribers")
            };

            if (channel.Badge == "")
            {
                channel.Badge = null;
            }
            return(channel);
        }
Esempio n. 9
0
        public string ListVideos(YouTubeVideoOptions options)
        {
            // Initialize the query
            NameValueCollection query = new NameValueCollection {
                { "part", "id,snippet,contentDetails,fileDetails,liveStreamingDetails,player,processingDetails,recordingDetails,statistics,status,suggestions,topicDetails" },
                { "access_token", Client.AccessToken },
                { "key", Client.ApiKey }
            };

            // Optional parameters
            if (options != null)
            {
                if (options.Ids != null && options.Ids.Length > 0)
                {
                    query.Add("id", String.Join(",", options.Ids));
                }
                if (options.MaxResults > 0)
                {
                    query.Add("maxResults", options.MaxResults + "");
                }
                if (!String.IsNullOrWhiteSpace(options.PageToken))
                {
                    query.Add("pageToken", options.PageToken);
                }
            }

            // Make the call to the API
            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://www.googleapis.com/youtube/v3/videos", query));
        }
Esempio n. 10
0
        /// <summary>
        /// Posts a link with the specified options.
        /// </summary>
        /// <param name="identifier">The identifier of user, page or similar.</param>
        /// <param name="options">The options for the link.</param>
        public string PostLink(string identifier, FacebookPostLinkOptions options)
        {
            // Construct the query string
            NameValueCollection query = new NameValueCollection();

            if (!String.IsNullOrWhiteSpace(options.Link))
            {
                query.Add("link", options.Link);
            }
            if (!String.IsNullOrWhiteSpace(options.Description))
            {
                query.Add("description", options.Description);
            }
            if (!String.IsNullOrWhiteSpace(options.Message))
            {
                query.Add("message", options.Message);
            }
            if (!String.IsNullOrWhiteSpace(options.Name))
            {
                query.Add("name", options.Name);
            }
            if (!String.IsNullOrWhiteSpace(options.Caption))
            {
                query.Add("caption", options.Caption);
            }
            query.Add("access_token", Client.AccessToken);

            // Make the call to the API
            return(SocialUtils.DoHttpPostRequestAndGetBodyAsString("https://graph.facebook.com/" + identifier + "/feed", query));
        }
Esempio n. 11
0
        /// <summary>
        /// Search for media in a given area. The default time span is set to 5 days. The time span must not
        /// exceed 7 days. Defaults time stamps cover the last 5 days. Can return mix of image and video types.
        /// </summary>
        /// <param name="options">The search options.</param>
        public string Search(InstagramRecentMediaSearchOptions options)
        {
            // Declare the query string
            NameValueCollection qs = new NameValueCollection {
                { "access_token", Client.AccessToken },
                { "lat", options.Latitude.ToString(CultureInfo.InvariantCulture) },
                { "lng", options.Longitude.ToString(CultureInfo.InvariantCulture) }
            };

            // Optinal options
            if (options.Distance > 0)
            {
                qs.Add("distance", options.Distance + "");
            }
            if (options.MinTimestamp > 0)
            {
                qs.Add("min_timestamp", options.MinTimestamp + "");
            }
            if (options.MaxTimestamp > 0)
            {
                qs.Add("max_timestamp", options.MaxTimestamp + "");
            }

            // Perform the call to the API
            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://api.instagram.com/v1/media/search", qs));
        }
Esempio n. 12
0
        internal string GenerateAbsoluteUrl(string relative, NameValueCollection query)
        {
            string url = "https://api.github.com";

            if (Credentials != null)
            {
                url = "https://" + Credentials.UserName + ":" + Credentials.Password + "@api.github.com";
            }
            else if (AccessToken != null)
            {
                query.Add("access_token", AccessToken);
            }

            // Add the relative URL
            url += relative;

            // Append the query string (if not empty)
            if (query != null && query.Count > 0)
            {
                url += "?" + SocialUtils.NameValueCollectionToQueryString(query);
            }

            // Now return the URL
            return(url);
        }
Esempio n. 13
0
        public string GetAuthorizationUrl(string state, GitHubScopeCollection scopes)
        {
            // Initialize the query string
            NameValueCollection nvc = new NameValueCollection {
                { "client_id", ClientId }
            };

            // Add the redirect URI if specified
            if (!String.IsNullOrWhiteSpace(RedirectUri))
            {
                nvc.Add("redirect_uri", RedirectUri);
            }

            // Add the state if specified
            if (!String.IsNullOrWhiteSpace(state))
            {
                nvc.Add("state", state);
            }

            // Get the scope list
            string scope = (scopes == null ? "" : scopes.ToString());

            if (!String.IsNullOrWhiteSpace(scope))
            {
                nvc.Add("scope", scope);
            }

            // Generate the URL
            return("https://github.com/login/oauth/authorize?" + SocialUtils.NameValueCollectionToQueryString(nvc));
        }
Esempio n. 14
0
        /// <summary>
        /// Makes a signed request to the Twitter API based on the specified parameters.
        /// </summary>
        /// <param name="method">The HTTP method of the request.</param>
        /// <param name="url">The base URL of the request (no query string).</param>
        /// <param name="queryString">The query string.</param>
        /// <param name="postData">The POST data.</param>
        public virtual HttpWebResponse DoHttpRequest(string method, string url, NameValueCollection queryString, NameValueCollection postData)
        {
            // Check if NULL
            if (queryString == null)
            {
                queryString = new NameValueCollection();
            }
            if (postData == null)
            {
                postData = new NameValueCollection();
            }

            // Merge the query string
            if (queryString.Count > 0)
            {
                UriBuilder builder = new UriBuilder(url);
                url += (url.Contains("?") ? "&" : "") + builder.MergeQueryString(queryString).Query;
            }

            // Initialize the request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            // Generate the signature
            string signature = GenerateSignature(method, url, queryString, postData);

            // Generate the header
            string header = GenerateHeaderString(signature);

            // Add the authorization header
            request.Headers.Add("Authorization", header);

            // Set the method
            request.Method = method;

            // Add the request body (if a POST request)
            if (method == "POST" && postData.Count > 0)
            {
                string dataString = SocialUtils.NameValueCollectionToQueryString(postData);
                //throw new Exception(dataString);
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = dataString.Length;
                using (Stream stream = request.GetRequestStream()) {
                    stream.Write(Encoding.UTF8.GetBytes(dataString), 0, dataString.Length);
                }
            }

            // Make sure we reset the client (timestamp and nonce)
            if (AutoReset)
            {
                Reset();
            }

            // Get the response
            try {
                return((HttpWebResponse)request.GetResponse());
            } catch (WebException ex) {
                return((HttpWebResponse)ex.Response);
            }
        }
Esempio n. 15
0
 public string GetMyPlaylists(YouTubePlaylistPartsCollection parts)
 {
     return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://www.googleapis.com/youtube/v3/playlists", new NameValueCollection {
         { "part", parts.ToString() },
         { "mine", "true" },
         { "access_token", Client.AccessToken }
     }));
 }
Esempio n. 16
0
 public string GetPlaylistsFromChannel(string channelId, YouTubePlaylistPartsCollection parts)
 {
     return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://www.googleapis.com/youtube/v3/playlists", new NameValueCollection {
         { "part", parts.ToString() },
         { "channelId", channelId },
         { "access_token", Client.AccessToken }
     }));
 }
Esempio n. 17
0
 public string GetPlaylistsFromIds(string[] playlistIds, YouTubePlaylistPartsCollection parts)
 {
     return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://www.googleapis.com/youtube/v3/playlists", new NameValueCollection {
         { "part", parts.ToString() },
         { "id", String.Join(",", playlistIds) },
         { "access_token", Client.AccessToken }
     }));
 }
Esempio n. 18
0
 public static UriBuilder MergeQueryString(this UriBuilder builder, NameValueCollection values)
 {
     if (values == null || values.Count == 0)
     {
         return(builder);
     }
     builder.Query = SocialUtils.NameValueCollectionToQueryString(HttpUtility.ParseQueryString(builder.Query).Set(values));
     return(builder);
 }
Esempio n. 19
0
        /// <summary>
        /// Gets information about a media object.
        /// </summary>
        /// <param name="mediaId">The ID of the media.</param>
        public string GetMedia(string mediaId)
        {
            // Declare the query string
            NameValueCollection qs = new NameValueCollection {
                { "access_token", Client.AccessToken }
            };

            // Perform the call to the API
            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://api.instagram.com/v1/media/" + mediaId, qs));
        }
        public string GetUserInfo()
        {
            // Declare the query string
            NameValueCollection query = new NameValueCollection {
                { "access_token", AccessToken },
            };

            // Make a call to the server
            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://www.googleapis.com/oauth2/v3/userinfo", query));
        }
Esempio n. 21
0
        public VimeoChannelsResponse GetUserChannels(string name)
        {
            HttpWebResponse response = SocialUtils.DoHttpGetRequest("http://vimeo.com/api/v2/" + name + "/channels.xml");

            if (response.StatusCode == HttpStatusCode.OK)
            {
                return(VimeoChannelsResponse.Parse(XElement.Parse(response.GetAsString())));
            }
            throw new VimeoException(response.GetAsString());
        }
Esempio n. 22
0
        public VimeoChannel GetChannelInfo(string name)
        {
            HttpWebResponse response = SocialUtils.DoHttpGetRequest("http://vimeo.com/api/v2/channel/" + name + "/info.xml");

            if (response.StatusCode == HttpStatusCode.OK)
            {
                return(VimeoChannel.ParseMultiple(XElement.Parse(response.GetAsString())).FirstOrDefault());
            }
            throw new VimeoException(response.GetAsString());
        }
Esempio n. 23
0
        public string GetMyChannels(YouTubeChannelPartsCollection parts)
        {
            NameValueCollection query = new NameValueCollection();

            query.Add("part", parts.ToString());
            query.Add("mine", "true");
            query.Add("access_token", Client.AccessToken);

            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://www.googleapis.com/youtube/v3/channels", query));
        }
Esempio n. 24
0
        public string GetChannelVideosAsRawJson(string id, int page)
        {
            NameValueCollection query = new NameValueCollection();

            if (page > 0)
            {
                query.Set("page", page + "");
            }
            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("http://vimeo.com/api/v2/channel/" + id + "/videos.json", query));
        }
Esempio n. 25
0
        public string GetVideoDetails(string id)
        {
            NameValueCollection query = new NameValueCollection();

            query.Add("id", id);
            query.Add("part", "snippet,contentDetails,fileDetails,player,processingDetails,recordingDetails,statistics,status,suggestions,topicDetails");
            query.Add("access_token", Client.AccessToken);

            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://www.googleapis.com/youtube/v3/videos", query));
        }
Esempio n. 26
0
        /// <summary>
        /// Gets a status message with the specified ID.
        /// </summary>
        /// <param name="statusMessageId">The ID of the status message.</param>
        public string GetStatusMessage(string statusMessageId)
        {
            // Construct the query string
            NameValueCollection query = new NameValueCollection {
                { "access_token", Client.AccessToken }
            };

            // Make the call to the API
            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://graph.facebook.com/" + statusMessageId, query));
        }
Esempio n. 27
0
        /// <summary>
        /// Posts a status message to the wall of specified <var>identifier</var>.
        /// </summary>
        /// <param name="identifier">The identifier of the user, page or similar.</param>
        /// <param name="message">The text of the status message.</param>
        public string PostStatusMessage(string identifier, string message)
        {
            // Construct the query string
            NameValueCollection query = new NameValueCollection {
                { "message", message },
                { "access_token", Client.AccessToken }
            };

            // Make the call to the API
            return(SocialUtils.DoHttpPostRequestAndGetBodyAsString("https://graph.facebook.com/" + identifier + "/feed", query));
        }
Esempio n. 28
0
        public string GetChannelForUsername(YouTubeChannelPartsCollection parts, string userName)
        {
            NameValueCollection query = new NameValueCollection();

            query.Add("part", parts.ToString());
            query.Add("forUsername", userName);
            query.Add("access_token", Client.AccessToken);
            query.Add("key", Client.ApiKey);

            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://www.googleapis.com/youtube/v3/channels", query));
        }
Esempio n. 29
0
        /// <summary>
        /// Gets debug information about the specified access token.
        /// </summary>
        /// <param name="accessToken">The access token to debug.</param>
        /// <returns>The raw JSON response from the API.</returns>
        public string DebugToken(string accessToken)
        {
            // Declare the query string
            NameValueCollection query = new NameValueCollection {
                { "input_token", accessToken },
                { "access_token", Client.AccessToken }
            };

            // Make the call to the API
            return(SocialUtils.DoHttpGetRequestAndGetBodyAsString("https://graph.facebook.com/debug_token", query));
        }
Esempio n. 30
0
        public static void AppendQueryString(this UriBuilder builder, NameValueCollection values)
        {
            if (values == null || values.Count == 0)
            {
                return;
            }
            NameValueCollection nvc = HttpUtility.ParseQueryString(builder.Query);

            nvc.Add(values);
            builder.Query = SocialUtils.NameValueCollectionToQueryString(nvc);
        }