Facebook Unity API - post screenshot with link and description?

房东的猫 提交于 2019-12-22 18:38:19

问题


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?


回答1:


    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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/20228436/facebook-unity-api-post-screenshot-with-link-and-description

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!