Exemple #1
0
        public async Task testUploadPhoto()
        {
            string token = await getAppToken();

            PropertySet parameters = new PropertySet();

            parameters.Add("access_token", Uri.EscapeUriString(token));
            parameters.Add("permissions", FBTestPhotoUploadPermissions);

            FBTestUser user = await createTestUser(parameters);

            StorageFolder appFolder =
                Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFile f = await appFolder.GetFileAsync(
                FBTestImagePath);

            IRandomAccessStreamWithContentType stream = await f.OpenReadAsync();

            Assert.IsNotNull(stream);

            FBMediaStream fbStream = new FBMediaStream(FBTestImageName,
                                                       stream);

            // Switch to user access token to post photo.
            parameters.Remove("access_token");
            parameters.Add("access_token", user.AccessToken);

            parameters.Add("source", fbStream);

            string path = "/" + user.Id + "/photos";

            FBSingleValue sval = new FBSingleValue(path, parameters,
                                                   new FBJsonClassFactory(FBPhoto.FromJson));

            FBResult result = await sval.PostAsync();

            Assert.IsTrue(result.Succeeded);

            try
            {
                FBPhoto pic = (FBPhoto)result.Object;
            }
            catch (InvalidCastException)
            {
                Assert.IsFalse(true, "Object returned was not of the " +
                               "expected type (FBPhoto).");
            }
        }
Exemple #2
0
        public async Task <FBResult> publishCustomUserObject(
            FBTestUser user
            )
        {
            PropertySet parameters = new PropertySet();
            string      path       = user.Id + "/objects/logincs:noun";

            Assert.IsNotNull(user.AccessToken);
            parameters.Add("access_token", user.AccessToken);
            parameters.Add("object", FBCustomObjectInstance);

            FBSingleValue sval = new FBSingleValue(path, parameters,
                                                   new FBJsonClassFactory(FBObject.FromJson));

            return(await sval.PostAsync());
        }
Exemple #3
0
        public async Task <FBResult> publishCustomStory(
            FBTestUser user,
            FBObject customObject
            )
        {
            PropertySet parameters = new PropertySet();
            string      path       = user.Id + "/logincs:verb";

            parameters.Add("noun", customObject.Id);
            parameters.Add("access_token", user.AccessToken);

            FBSingleValue sval = new FBSingleValue(path, parameters,
                                                   new FBJsonClassFactory(FBObject.FromJson));

            return(await sval.PostAsync());
        }
Exemple #4
0
        private async Task <FBTestUser> createTestUser(
            PropertySet Parameters
            )
        {
            FBTestUser user    = null;
            bool       success = false;

            // If you're creating/deleting test users frequently, like say in
            // a unit test suite, Facebook will occasionally fail this call with
            // the error code 1, type "OAuthException" and the message "An
            // unknown error occurred".  Throwing in a short delay and retrying
            // almost always alleviates the problem, so adding a few retries
            // here greatly increases the robustness of the test suite.
            for (int retries = 0; (success == false) && (retries < 5);
                 retries++)
            {
                string path = "/" + TestAppId + FBSDKTestUsersPath;

                FBSingleValue sval = new FBSingleValue(path, Parameters,
                                                       new FBJsonClassFactory(FBTestUser.FromJson));

                FBResult fbresult = await sval.PostAsync();

                if ((fbresult.Succeeded == false) || (fbresult.Object == null))
                {
                    await Task.Delay(TEST_RETRY_DELAY);
                }
                else
                {
                    try
                    {
                        user    = (FBTestUser)fbresult.Object;
                        success = true;
                    }
                    catch (InvalidCastException)
                    {
                        Assert.IsTrue(false, "Item returned is not expected type" +
                                      " (FBTestUser)");
                    }
                }
            }

            Assert.IsNotNull(user);

            return(user);
        }
Exemple #5
0
        private static IAsyncOperation <FBResult> ExecuteAsync(string method, FBSingleValue request)
        {
            switch (method.ToLowerInvariant())
            {
            case "delete":
                return(request.DeleteAsync());

            case "get":
                return(request.GetAsync());

            case "post":
                return(request.PostAsync());

            default:
                throw new NotSupportedException($"Method '{method}' is not supported.");
            }
        }
Exemple #6
0
        private async Task <FBObject> postToFeed(
            FBTestUser User,
            string Message
            )
        {
            PropertySet parameters = new PropertySet();

            parameters.Add("access_token", User.AccessToken);
            parameters.Add("message", Message);

            string path = "/" + User.Id + FBFeedPath;

            FBSingleValue sval = new FBSingleValue(path, parameters,
                                                   new FBJsonClassFactory(FBObject.FromJson));

            FBResult fbresult = await sval.PostAsync();

            return((FBObject)fbresult.Object);
        }
        public async Task <string> PostPictureToFeedAsync(string title, string pictureName, IRandomAccessStreamWithContentType pictureStream)
        {
            if (pictureStream == null)
            {
                return(null);
            }

            if (Provider.LoggedIn)
            {
                var facebookPictureStream = new FBMediaStream(pictureName, pictureStream);
                var parameters            = new PropertySet
                {
                    { "source", facebookPictureStream },
                    { "name", title }
                };

                string path    = FBSession.ActiveSession.User.Id + "/photos";
                var    factory = new FBJsonClassFactory(JsonConvert.DeserializeObject <FacebookPicture>);

                var singleValue = new FBSingleValue(path, parameters, factory);
                var result      = await singleValue.PostAsync();

                if (result.Succeeded)
                {
                    var photoResponse = result.Object as FacebookPicture;
                    if (photoResponse != null)
                    {
                        return(photoResponse.Id);
                    }
                }

                return(null);
            }

            var isLoggedIn = await LoginAsync();

            if (isLoggedIn)
            {
                return(await PostPictureToFeedAsync(title, pictureName, pictureStream));
            }

            return(null);
        }
Exemple #8
0
        /// <summary>
        /// Enables direct posting data to the timeline.
        /// </summary>
        /// <param name="title">Title of the post.</param>
        /// <param name="message">Message of the post.</param>
        /// <param name="description">Description of the post.</param>
        /// <param name="link">Link contained as part of the post. Cannot be null</param>
        /// <param name="pictureUrl">URL of a picture attached to this post. Can be null</param>
        /// <returns>Task to support await of async call.</returns>
        public async Task <bool> PostToFeedAsync(string title, string message, string description, string link, string pictureUrl = null)
        {
            if (Provider.LoggedIn)
            {
                var parameters = new PropertySet {
                    { "title", title }, { "message", link }, { "description", description }, { "link", link }
                };

                if (!string.IsNullOrEmpty(pictureUrl))
                {
                    parameters.Add(new KeyValuePair <string, object>("picture", pictureUrl));
                }

                string path    = FBSession.ActiveSession.User.Id + "/feed";
                var    factory = new FBJsonClassFactory(JsonConvert.DeserializeObject <FacebookPost>);

                var singleValue = new FBSingleValue(path, parameters, factory);
                var result      = await singleValue.PostAsync();

                if (result.Succeeded)
                {
                    var postResponse = result.Object as FacebookPost;
                    if (postResponse != null)
                    {
                        return(true);
                    }
                }

                Debug.WriteLine(string.Format("Could not post. {0}", result.ErrorInfo?.ErrorUserMessage));
                return(false);
            }

            var isLoggedIn = await LoginAsync();

            if (isLoggedIn)
            {
                return(await PostToFeedAsync(title, message, description, link, pictureUrl));
            }

            return(false);
        }
Exemple #9
0
        public async Task testLikeSomething()
        {
            string token = await getAppToken();

            PropertySet parameters = new PropertySet();

            parameters.Add("access_token", Uri.EscapeUriString(token));
            parameters.Add("permissions",
                           "public_profile,publish_actions,user_photos");

            FBTestUser user = await createTestUser(parameters);

            string path = user.Id + "/og.likes";

            // Because *everybody* likes these, amirite?
            string likedObject =
                Uri.EscapeUriString("http://en.wikipedia.org/wiki/Brussels_sprout");

            parameters.Add("object", likedObject);

            FBSingleValue sval = new FBSingleValue(path, parameters,
                                                   new FBJsonClassFactory(FBObject.FromJson));

            FBResult result = await sval.PostAsync();

            Assert.IsTrue(result.Succeeded);

            try
            {
                FBObject like = (FBObject)result.Object;
            }
            catch (InvalidCastException)
            {
                Assert.IsFalse(true, "Object returned was not of the " +
                               "expected type (FBObject).");
            }
        }
        public async Task <bool> PostToFeedAsync(string link)
        {
            if (Provider.LoggedIn)
            {
                var parameters = new PropertySet {
                    { "link", link }
                };

                string path    = FBSession.ActiveSession.User.Id + "/feed";
                var    factory = new FBJsonClassFactory(JsonConvert.DeserializeObject <FacebookPost>);

                var singleValue = new FBSingleValue(path, parameters, factory);
                var result      = await singleValue.PostAsync();

                if (result.Succeeded)
                {
                    var postResponse = result.Object as FacebookPost;
                    if (postResponse != null)
                    {
                        return(true);
                    }
                }

                Debug.WriteLine(string.Format("Could not post. {0}", result.ErrorInfo?.ErrorUserMessage));
                return(false);
            }

            var isLoggedIn = await LoginAsync();

            if (isLoggedIn)
            {
                return(await PostToFeedAsync(link));
            }

            return(false);
        }
Exemple #11
0
        public async Task<FBResult> publishCustomStory(
            FBTestUser user,
            FBObject customObject
            )
        {
            PropertySet parameters = new PropertySet();
            string path = user.Id + "/logincs:verb";

            parameters.Add("noun", customObject.Id);
            parameters.Add("access_token", user.AccessToken);

            FBSingleValue sval = new FBSingleValue(path, parameters,
                new FBJsonClassFactory(FBObject.FromJson));

            return await sval.PostAsync();
        }
Exemple #12
0
        public async Task<FBResult> publishCustomUserObject(
            FBTestUser user
            )
        {
            PropertySet parameters = new PropertySet();
            string path = user.Id + "/objects/logincs:noun";

            Assert.IsNotNull(user.AccessToken);
            parameters.Add("access_token", user.AccessToken);
            parameters.Add("object", FBCustomObjectInstance);

            FBSingleValue sval = new FBSingleValue(path, parameters,
                new FBJsonClassFactory(FBObject.FromJson));

            return await sval.PostAsync();
        }
Exemple #13
0
        private async Task<FBObject> postToFeed(
            FBTestUser User,
            string Message
            )
        {
            PropertySet parameters = new PropertySet();

            parameters.Add("access_token", User.AccessToken);
            parameters.Add("message", Message);

            string path = "/" + User.Id + FBFeedPath;

            FBSingleValue sval = new FBSingleValue(path, parameters,
                new FBJsonClassFactory(FBObject.FromJson));

            FBResult fbresult = await sval.PostAsync();

            return (FBObject)fbresult.Object;
        }
Exemple #14
0
        private async Task<FBTestUser> createTestUser(
            PropertySet Parameters 
            )
        {
            FBTestUser user = null;
            bool success = false;

            // If you're creating/deleting test users frequently, like say in
            // a unit test suite, Facebook will occasionally fail this call with 
            // the error code 1, type "OAuthException" and the message "An 
            // unknown error occurred".  Throwing in a short delay and retrying
            // almost always alleviates the problem, so adding a few retries
            // here greatly increases the robustness of the test suite.
            for (int retries = 0; (success == false) && (retries < 5); 
                retries++)
            {
                string path = "/" + TestAppId + FBSDKTestUsersPath;

                FBSingleValue sval = new FBSingleValue(path, Parameters,
                    new FBJsonClassFactory(FBTestUser.FromJson));

                FBResult fbresult = await sval.PostAsync();
    
                if ((fbresult.Succeeded == false) || (fbresult.Object == null))
                {
                    await Task.Delay(TEST_RETRY_DELAY);
                }
                else
                {
                    try
                    {
                        user = (FBTestUser)fbresult.Object;
                        success = true;
                    }
                    catch (InvalidCastException)
                    {
                        Assert.IsTrue(false, "Item returned is not expected type" +
                            " (FBTestUser)");
                    }
                }
            }

            Assert.IsNotNull(user);

            return user;
        }
Exemple #15
0
        private static async void FacebookPoster()
        {
            // Get active session
            FBSession sess = FBSession.ActiveSession;


            if (sess.LoggedIn)
            {
                var user = sess.User;
                // Set caption, link and description parameters
                var parameters = new PropertySet();

                // Add post message
                await LocationAccesser();
                parameters.Add("message", _message + "\n" + "\n" + _latitude + "\n" + _longitude);

                // Set Graph api path
                var path = "/" + user.Id + "/feed";

                var factory = new FBJsonClassFactory(s => {
                    return JsonConvert.DeserializeObject<FBReturnObject>(s);
                });

                var singleValue = new FBSingleValue(path, parameters, factory);
                var result = await singleValue.PostAsync();
                if (result.Succeeded)
                {
                    Debug.WriteLine("Succeed");
                }
                else
                {
                    Debug.WriteLine("Failed");
                }
            }
        }
Exemple #16
0
        public async Task testUploadPhoto()
        {
            string token = await getAppToken();
            PropertySet parameters = new PropertySet();

            parameters.Add("access_token", Uri.EscapeUriString(token));
            parameters.Add("permissions", FBTestPhotoUploadPermissions);

            FBTestUser user = await createTestUser(parameters);

            StorageFolder appFolder = 
                Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFile f = await appFolder.GetFileAsync(
                FBTestImagePath);
            IRandomAccessStreamWithContentType stream = await f.OpenReadAsync();
            Assert.IsNotNull(stream);

            FBMediaStream fbStream = new FBMediaStream(FBTestImageName, 
                stream);

            // Switch to user access token to post photo.
            parameters.Remove("access_token");
            parameters.Add("access_token", user.AccessToken);

            parameters.Add("source", fbStream);

            string path = "/" + user.Id + "/photos";

            FBSingleValue sval = new FBSingleValue(path, parameters,
                new FBJsonClassFactory(FBPhoto.FromJson));

            FBResult result = await sval.PostAsync();
            Assert.IsTrue(result.Succeeded);

            try
            {
                FBPhoto pic = (FBPhoto)result.Object;
            }
            catch (InvalidCastException)
            {
                Assert.IsFalse(true, "Object returned was not of the " +
                    "expected type (FBPhoto).");
            }
        }
        private async Task FacebookPoster()
        {
            // Get active session
            FBSession sess = FBSession.ActiveSession;

            SosPageText += "\n Getting active Facebook Session...";
            RaisePropertyChanged(() => SosPageText);

            if (sess.LoggedIn)
            {
                var user = sess.User;
                // Set caption, link and description parameters
                var parameters = new PropertySet();

                // Add post message
                await LocationAccesser();
                parameters.Add("message", Message + "\n" + "\n" + _latitude + "\n" + _longitude);

                // Set Graph api path
                var path = "/" + user.Id + "/feed";

                var factory = new FBJsonClassFactory(JsonConvert.DeserializeObject<FBReturnObject>);

                var singleValue = new FBSingleValue(path, parameters, factory);
                var result = await singleValue.PostAsync();
                if (result.Succeeded)
                {
                    SosPageText += "\n Posted to Facebook Wall \n";
                }
                else
                {
                    SosPageText += "\n Can't post to Facebook Wall \n";
                }
            }
            else
            {
                SosPageText += "\n Facebook Not Configured or Active session not available! ";
                RaisePropertyChanged(() => SosPageText);
            }
            RaisePropertyChanged(()=>SosPageText);
        }
Exemple #18
0
        public async Task testLikeSomething()
        {
            string token = await getAppToken();
            PropertySet parameters = new PropertySet();

            parameters.Add("access_token", Uri.EscapeUriString(token));
            parameters.Add("permissions",
                "public_profile,publish_actions,user_photos");

            FBTestUser user = await createTestUser(parameters);

            string path = user.Id + "/og.likes";

            // Because *everybody* likes these, amirite?
            string likedObject =
                Uri.EscapeUriString("http://en.wikipedia.org/wiki/Brussels_sprout");

            parameters.Add("object", likedObject);

            FBSingleValue sval = new FBSingleValue(path, parameters,
                new FBJsonClassFactory(FBObject.FromJson));

            FBResult result = await sval.PostAsync();
            Assert.IsTrue(result.Succeeded);

            try
            {
                FBObject like = (FBObject)result.Object;
            }
            catch (InvalidCastException)
            {
                Assert.IsFalse(true, "Object returned was not of the " +
                    "expected type (FBObject).");
            }
        }
Exemple #19
0
 private static IAsyncOperation<FBResult> ExecuteAsync(string method, FBSingleValue request)
 {
     switch (method.ToLowerInvariant())
     {
         case "delete":
             return request.DeleteAsync();
         case "get":
             return request.GetAsync();
         case "post":
             return request.PostAsync();
         default:
             throw new NotSupportedException($"Method '{method}' is not supported.");
     }
 }