2

I am trying to get my app (iOS, Android) to allow users to post a screenshot to facebook with a link and a description. I am able to use FB.API() to upload screenshots from my app to a user's album that Facebook autogenerated for my app, via:

    int width = Screen.width;
    int height = Screen.height;
    Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

    // Read screen contents into the texture
    tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);

    tex.Apply();
    byte[] screenshot = tex.EncodeToPNG();

    var wwwForm = new WWWForm();

    string picName = "Idioman_" + Time.time + ".png";
    wwwForm.AddBinaryData("image", screenshot, picName);

    Debug.Log("trying to post screenshot");
    FB.API("me/photos", Facebook.HttpMethod.POST, PostPicCallback, wwwForm); 

And I am able to use FB.Feed() to post an image from the internet with a link and a description to a user's feed. Is there a way to post the screenshot to a user's feed with a link and a description?

JeanLuc
  • 4,783
  • 1
  • 33
  • 47
TegTap
  • 31
  • 1
  • 1
  • 2
  • What have you tried so far? What is wrong with the code you pasted already? – S.Richmond Nov 27 '13 at 02:25
  • 1
    The code above works, in that it posts an image to the user's album. What's "wrong" is that I don't see a way to create a post that includes the screenshot, a link, and a description. FB.API only takes the 4 inputs shown above. I've tried using FB.Feed, and I'm able to post an image with a link and a user-editable message, but it only accepts a URL for the picture. The picture must already exist on the internet - FB.Feed doesn't provide a way to post the Texture2D that was captured. – TegTap Nov 27 '13 at 03:15
  • I tried to find a way to post the screenshot via FB.API (as in the code I provided), and then provide the URL to that screenshot as an input to FB.Feed so I could use FB.Feed's ability to have a link and a description in the post. The problem with that method is I can't get the URL to that screenshot once it's posted. I can't find a rhyme or reason to facebook's photo naming convention. I've looked everywhere for a solution. The key is taking a screenshot, and posting it with a description and a link. – TegTap Nov 27 '13 at 03:34

3 Answers3

2
    var snap = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    snap.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    snap.Apply();
    var screenshot = snap.EncodeToPNG();

    int i = UnityEngine.Random.Range (0, 2);

    var wwwForm = new WWWForm();
    wwwForm.AddBinaryData("image", screenshot, "picture.png");
    wwwForm.AddField ("name", "this will be the caption for the image");

    FB.API("me/photos", HttpMethod.POST, CallbackUploadImage, wwwForm);

you can refer here for more details of the available fields

https://developers.facebook.com/docs/graph-api/reference/v2.2/photo

Ismi Ammar
  • 166
  • 1
  • 11
1

After you upload the screenshot using your code above, check the FBResult from your callback method and parse the result with key "id" so you got your uploaded photo id.

Your photo link will be "https://www.facebook.com/photo.php?fbid=INSERT_YOUR_ID" as the INSERT_YOUR_ID is the id from the result before. Use that link on FB.Feed.

chikebum
  • 11
  • 1
0

Follow these steps:

  1. First login using FB.LogInWithPublishPermissions by adding the "publish_actions" permission in parameters.
  2. Use Facebook Graph API to upload the image.

For more details link is here.

iamdanchiv
  • 4,052
  • 4
  • 37
  • 42