Using Facebook Unity SDK, how to post a custom story with a screenshot on iOS
Asked Answered
A

1

0

How do you post a custom story via the Facebook Unity SDK with a screenshot attached?

What I did:

Followed smash Unity tutorial. Created profile object. Created action smash. Created story smash + profile. Saved.

1) It says Unable to Generate Story. Is this a problem or can I still test my story?

Then based on the code from the tutorial, I wrote:

public void publishAction()
{
Debug.Log ("publish action.");
if (FB.IsLoggedIn)
{
Dictionary<string, string> querySmash = new Dictionary<string, string>();
string testUserID = "1378641979";
querySmash["profile"] = testUserID;
FB.API ("/me/" + FB.AppId + ":smash", Facebook.HttpMethod.POST, publishActionCallback, querySmash);
}
}

void publishActionCallback(FBResult result)
{
if (result.Error != null)
{
Debug.LogWarning("FacebookManager-publishActionCallback: error: " + result.Error );
}
else
{
Debug.Log("FacebookManager-publishActionCallback: success: " + result.Text );
}
}

2) This code always return 400 Bad Request. Not sure why as it is a copy of the tutorial example. Any idea?

Next step, I would like to attached a screenshot of the game to this story. I do not have a web site to host it. I know how to get the screenshot.

var width = Screen.width;
var height = Screen.height;
var 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();

3) The question is how do I attach it to my custom story?

4) I am still unclear after doing some research on how to give my custom story a custom title and message. Please let me know how.

5) Finally, if my object is a new word (that is not predefined like profile is), what would my API call look like? Let's say custom story object is "meal".

Would it be: querySmash["meal"] = "A delicious pizza!"?

Thanks for your help!

Appellee answered 2/2, 2014 at 20:28 Comment(0)
D
2

I'll take these questions one at a time:

#1 - 2. When you specify the action name, you need to use namespace:*action* rather than appid:action. To see if that is the problem, see if the body of that 400 error reads something like "invalid path components".

#3. In your action configuration, create an action property whose type is image. Then, whatever the name of that property is, use that as the key and your image bytes as the value in a WWWForm, which you pass into FB.API() as its formData argument (see the docs for details). If you created a property called "screenpic", your example might continue something like

var wwwForm = new WWWForm();
wwwForm.AddBinaryData("screenpic", screenshot, "temp.png");

FB.API("me/mynamespace:myaction", Facebook.HttpMethod.POST, Callback, wwwForm);

#4. You configure your title and message using the "Edit attachments" button on the Story configuration, possibly using templates to include strings provided either as part of the object's markup or as action parameters, as described more completely in the "Creating Custom Stories" docs.

#5. Almost. If you look at how you handled the case of a built-in object, you'll see that what you provided a URL as the value of profile. If you load up that URL in Facebook's [Open Graph Debugger][4], you'll see that the web page at that URL has been marked up with Open Graph metadata. So, similarly, in this case, you'd need to create a web page with the appropriate Open Graph markup to define an object of your new type (one for each such object), and provide the URL of that object as the value of querySmash['meal"] in your example. The full rundown is in the "Using Objects" doc on the Facebook dev site.

Delora answered 5/2, 2014 at 0:32 Comment(3)
Hi Colin, Good news FB.API ("/me/mynamespace:smash", Facebook.HttpMethod.POST, publishActionCallback, querySmash) works. However, FB.API("me/mynamespace:smash", Facebook.HttpMethod.POST, Callback, wwwForm) returns a 400 Bad request even though I did create a "screenpic" image type property. Any idea? Also, since the smash example requires a profile querySmash["profile"] = testUserID; how can I send both the ID of the profile and the screenshot at the same time in the same FB.API call? Lastly as a suggestion for the SDK, add NAMESPACE in the settings and give access via FB.NAMESPACE. Thanks!Appellee
namespace probably does belong in the settings, and we'll take that into consideration in a forthcoming version of the SDK (if you submit a bug at developers.facebook.com/x/bugs/trending that will give you something to track). Without knowing the particular values you're passing into that function call, though, and the details of your configuration, it's hard to know precisely what's going wrong in your call. Again, if you submit a (separate) bug, you can include details you'd like to keep confidential but allow us to see if we can help, especially if there's a bug.Delora
Say Colin, @ColinCreitz thanks for this and sure hope you're still reading. I'm wondering how this is all affected by 2.1 which is coming Aug 8. I'm very confused about, simply, whether in fact it is even still possible to actually **post an image to a user's wall (from Unity or indeed any ios/android app) under 2.1"? Do you have any info on that?Rave

© 2022 - 2024 — McMap. All rights reserved.