从Unity上传图片到Facebook

4
我正在开发一个Unity游戏,你可以拍照并上传到Facebook,同时附带一些标签和其他内容(类似于Friendsmash)。问题是我没有可用于放置截图的Web服务器,并且Fb.Feeb(picture:)属性仅接受URL。
我已经了解到可以使用HTTP POST将图片发布到用户的图像中,然后在picture:中使用该链接,但我对HTTP POST一无所知,也无法弄清楚如何操作。
我还读到可以使用FB.API()来实现此操作,但我无法理解它。
非常感谢提供任何示例代码。
我的当前代码:
private string _path = "file://" + System.IO.Path.Combine(Application.persistentDataPath, "Images/image.png");

void Start ()
    {
        if (!FB.IsLoggedIn)
            FB.Login("email, publish_actions, publish_stream, user_photos", LoginCallback);
        StartCamera();
    }


private void OnBragClicked()

{
    FbDebug.Log("OnBragClicked");

//Post(); <-- dont know how

FB.Feed(
    linkCaption: "#hashtag",
    picture: "???",
    linkName: "Im hashtaging!",
    link: "https://apps.facebook.com/" + FB.AppId + "/?challenge_brag=" + (FB.IsLoggedIn ?  FB.UserId : "guest")
    );
}


 void TakeSnapshot()

{
    _snap = new Texture2D(_webCamTexture.width, _webCamTexture.height);
    _snap.SetPixels(_webCamTexture.GetPixels());
    _snap.Apply();

//System.IO.File.WriteAllBytes(_path, _snap.EncodeToPNG());
}
1个回答

4

Facebook SDK确实有一种方法可以实现这一点。您需要使用Fb.API()。以下是我所采用的方法:

private void TakeScreenshot()
{
    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();

    var wwwForm = new WWWForm();
    wwwForm.AddBinaryData("image", screenshot, "barcrawling.png");

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

一般来说,要添加某种标题,可以按照以下方式进行...
private byte[] postcardAsBytes;
string textMessage = "Play this great game at http://blah.com.";
Dictionary<string, object> d = new Dictionary<string, object>
  {
  { "message", textMessage },
  { "picture", postcardAsBytes }
  };
Facebook.instance.graphRequest(
        "me/photos", HTTPVerb.POST, d, yourCompletionHandler);
// in this example using the prime31 plugin, rather than the fb sdk

关键字段似乎是“message”。令人困惑的是,FB文档中记录的“dictionary”字段似乎无效,所以请首先尝试使用“message”字段。

有没有办法添加标题/预定义文本或描述呢? - Imran
我没有找到,抱歉。 - Reaver
@Imran 当然可以:wwwForm.AddField("message", "这里是您的消息"); wwwForm.AddField("description", "这里是您的描述"); - Sagiv b.g
嗨@UserQuestion - 谢谢你的回答,但问题是,这个方法在2016年8月8日之后还能用吗?有人知道吗?谢谢。我在这里问过了...http://stackoverflow.com/questions/38232224/facebook-post-to-wall-using-me-photos-in-2-1-facebook - Fattie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接