Search code examples
c#facebookimagesharingfile-sharing

How to share a bitmap on facebook as a photo in C#?


Ok so I am 100% new to sharing things from C# on facebook.

I need a sample code of how to share an Image on my facebook as a photo ?

Please do NOT answer with ( Google it, I dnt know, why ?, impossible, Facebook SDK )...


Solution

  • Your question is a bit vague. When you say "in C#", you mean in a web, a window, or a service environment? Because the way Facebook does it is that there has to be some authentication in the process, and that is achieved via redirecting to Facebook for a user to login, and THEN send the photo for sharing. It's quite a process, not just a one-line code that does magic.

    In any case, you have to do the following, and you have to figure out where you place it in your environment:

    1. Create a Facebook app that corresponds to your app; Facebook will then give you an app code, and an app secret code.
    2. Using the app code, redirect to Facebook to authenticate the user who wants the photo shared on their Facebook.
    3. Receive back a code from Facebook.
    4. Authorize your app by communicating to Facebook via a service call to allow photo sharing using the app code, the app secret code, and the code we got from step 2.
    5. Receive back a token from Facebook that will be used from now on to upload photos.
    6. NOW you can start uploading the photo via another service call to Facebook using that token.

    Here's the code:

    // Step 2: you have to research what TheScope will be from Facebook API that gives you access to photos
    Response.Redirect(string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&scope={1}&redirect_uri={2}"), MyAppCode, TheScope, MyRedirectingURL);
    
    // Step 3: this is on the `Page_Load` of MyRedirectingURL.
    // AnotherRedirectingURL will be your final destination on your app
    using (var wc = new WebClient())
    {
        string token = wc.DownloadString(string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&code={2}&redirect_uri={3}", MyAppCode, MyAppSecretCode, TheCode, AnotherRedirectingURL));   
    }
    
    // Step 4:  Use the token to start stream up or down
    using (var wc = new WebClient())
    {    
        Uri uploadUri = new Uri(string.Format("https://graph.facebook.com/{0}?{1}", PhotoUploadCommand, token));     
    
        // Find out what the PhotoUploadCommand is supposed to be from the Facebook API
        // use wc and uploadUri to upload the photo
    }
    

    Bottom line, you have to do your research on this... it's not that straightforward. It's the sad truth that I had to go through to do what you're doing.