Esempio n. 1
0
        public FacebookPublishResult PublishStatus(User user, string message)
        {
            using (var client = new HttpClient())
            {
                var result = new FacebookPublishResult();

                if (!hasFacebookToken(user))
                {
                    result.hasErrors = true;
                    result.errorMessage = badUserToken;
                }
                else {
                    var accessToken = user.FacebookToken;
                    client.BaseAddress = new Uri("https://graph.facebook.com");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    //HTTP GET

                    StringBuilder apiUri = new StringBuilder("/");
                    apiUri.Append(user.FacebookUserId);
                    apiUri.Append("/feed?");
                    apiUri.Append(accessToken);
                    apiUri.Append("&message=");
                    apiUri.Append(message);

                    var finalResponse = client.PostAsync(apiUri.ToString(), new StringContent("")).Result;
                    StreamContent respContent = (StreamContent)finalResponse.Content;
                    var respContentString = respContent.ReadAsStringAsync().Result;
                    dynamic jsonResp = Radabite.Backend.Helpers.JsonUtils.JsonObject.GetDynamicJsonObject(respContentString);

                    if (finalResponse.StatusCode == HttpStatusCode.OK && jsonResp.id != null)
                    {
                        result.hasErrors = false;
                    }
                    else
                    {
                        result.hasErrors = true;
                        result.errorMessage = jsonResp.errors.message;
                    }

                }
                return result;
            }
        }
Esempio n. 2
0
        public string GetProfilePictureUrl(User user)
        {
            using (var client = new HttpClient())
            {
                var result = new FacebookPublishResult();
                string url = "";
                if (!hasFacebookToken(user))
                {
                    result.hasErrors = true;
                    result.errorMessage = badUserToken;
                }
                else
                {
                    var accessToken = user.FacebookToken;
                    client.BaseAddress = new Uri("https://graph.facebook.com");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    //HTTP GET

                    StringBuilder apiUri = new StringBuilder("/");
                    apiUri.Append(user.FacebookUserId);
                    // redirect false to get the url as json instead of a redirect to the picture
                    // for size, can also do type=large instead of hard-coding height and width
                    apiUri.Append("/picture?height=300&width=300&redirect=false");
                    var response = client.GetAsync(apiUri.ToString()).Result;
                    var resString = response.Content.ReadAsStringAsync().Result;
                    dynamic resJson = Radabite.Backend.Helpers.JsonUtils.JsonObject.GetDynamicJsonObject(resString);
                    try {
                        url = resJson.data.url;
                    }
                    catch (RuntimeBinderException e) {
                        /* json wasn't as expected. carry on. */
                        /* if we cared more we would log this and fix the json parse */
                    }
                }
                return url;
            }
        }