Teemu Tapanila bio photo

Teemu Tapanila

CTO of Mallow

Email Twitter LinkedIn Github

This post continue from the last one: Get all of the friends from Facebook

So we are only going to touch the FacebookLogin.cs file. Lets start by FacebookLogin method.

We need to add user_photos into permissions. Because we want to access the photos of user. So now the FacebookLogin function will look like this

[sourcecode language=”csharp”] public FacebookLogin(Panel container, Page page) { _page = page; //Initialize _webBrowser _webBrowser = new WebBrowser(); //Get this from the facebook string appId = ""; //List of all the permissions you want to have access to //You can get the list of possiblities from here //http://developers.facebook.com/tools/explorer/ string[] extendedPermissions = new[] { "publish_stream", "offline_access", "user_groups", "user_photos" };

        var oauth = new FacebookOAuthClient { AppId = appId };
        //Telling the Facebook that we want token as response
        //and we are using touch enabled device
        var parameters = new Dictionary<string, object>
                    { "response_type", "token" },
                    { "display", "touch" }
        //If there's extended permissions build the string
        //and set it up
        if (extendedPermissions != null &&
            extendedPermissions.Length > 0)
            var scope = new StringBuilder();
            scope.Append(string.Join(",", extendedPermissions));
            parameters["scope"] = scope.ToString();
        //Create the login url
        var loginUrl = oauth.GetLoginUrl(parameters);
        //Add webBrowser to the contentPanel
        _webBrowser.Navigated += webBrowser_Navigated;
        //Open the facebook login page into the browser
    } [/sourcecode]

Also we are going to use the PhotoChooser because we are sending a photo to Facebook. So add this using line

[sourcecode language=”csharp”] using Microsoft.Phone.Tasks; [/sourcecode]

So when you have logged in into Facebook we are going to launch the Photo Chooser. After user has chosen a photo it will automatically post it into Facebook using description “This is a test image” and name it Couch.

[sourcecode language=”csharp”] private void GetInformation() { //Create the Facebook client with the accessToken. //after this the client can be used to retrieve/send the data FacebookClient _fbClient = new FacebookClient(_accessToken); //Create a eventhandler for data download completed _fbClient.GetCompleted += _fbClient_GetCompleted; //Get the friends data //_fbClient.GetAsync("me/friends"); //Create a new PhtoChooseTask var task = new PhotoChooserTask(); //Tell it to fire a event when completed task.Completed += TaskCompleted; //Show the chooser task.Show(); }

    void TaskCompleted(object sender, PhotoResult e)
        //Send the chose picture into PostPicture function

    private void PostPicture(Stream theFile)
        //Create a parameters Dictionary
        var parameters = new Dictionary<string, object>();
        var picture = new FacebookMediaObject
                //Tell facebook that we are sending image
                ContentType = "image/jpeg",
                //Give name to the image
                FileName = "Couch"
        //Create a new byteArray with right length
        var img = new byte[theFile.Length];
        //Convert the image content into bytearray
        theFile.Read(img, 0, img.Length);
        //Close the stream
        //Put the bytearray into Picture
        //Add the image into parameters
        parameters.Add("source", picture);
        //Description of the posted image
        parameters.Add("message", "This is a test image");
        //Create a new FB client
        var fbClient = new FacebookClient(_accessToken);
        //Which event to fireup once posting is completed
        fbClient.PostCompleted += FbClientPostCompleted;

    void FbClientPostCompleted(object sender, FacebookApiEventArgs e)
        //Just show simple messageBox indicating success
        _page.Dispatcher.BeginInvoke(() => MessageBox.Show("Success"));
    } [/sourcecode]

Full solution