- Home /
Posting screenshot on Facebook timeline using FB.API seems impossible
Hi all,
I am creating a game, and I would like the users to be able to post a screenshot (of score screen) to their timeline. I have tried many solutions in several topics, but get none of them working. After weeks of struggling hopefully someone can help me out.
Below is the code, which is called by the OnClick() event of a button.
using UnityEngine;
using System.Collections;
using Facebook.Unity;
using Facebook.MiniJSON;
public class FB_Test : MonoBehaviour {
// Awake function from Unity's MonoBehavior
void Awake()
{
if (!FB.IsInitialized)
{
// Initialize the Facebook SDK
FB.Init(InitCallback, OnHideUnity);
}
else {
// Already initialized, signal an app activation App Event
FB.ActivateApp();
}
}
private void InitCallback()
{
if (FB.IsInitialized)
{
// Signal an app activation App Event
FB.ActivateApp();
// Continue with Facebook SDK
// ...
}
else {
Debug.Log("Failed to Initialize the Facebook SDK");
}
}
private void OnHideUnity(bool isGameShown)
{
if (!isGameShown)
{
// Pause the game - we will need to hide
Time.timeScale = 0;
}
else {
// Resume the game - we're getting focus again
Time.timeScale = 1;
}
}
public void ShareFb()
{
StartCoroutine(TakeScreenshot());
}
private IEnumerator TakeScreenshot()
{
yield return new WaitForEndOfFrame();
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();
var wwwForm = new WWWForm();
wwwForm.AddBinaryData("image", screenshot, "Screenshot.png");
FB.API("me/photos", HttpMethod.POST, APICallback, wwwForm);
}
void APICallback(ILoginResult result)
{
if (result.Error != null)
lastResponse = "Error Response:\n" + result.Error;
else if (!FB.IsLoggedIn)
lastResponse = "Login cancelled by Player";
else
lastResponse = "Login was successful!";
}
}
I get the following errors when running this code:
Currently I am using: Facebook SDK version 7.5.0, and Unity version 5.3.1
Is there anyone who can tell me what I am doing wrong and send an example of how I can take a screenshot and post this to facebook timeline? Your help is highly appreciated.
Answer by steakpinball · Jun 24, 2016 at 03:30 PM
Your method APICallback
needs to accept IGraphResult
as a parameter.
void APICallback(IGraphResult result)
Fixing that will also resolve the error about converting WWWForm
to IDictionary
.
The variable lastResponse
is not declared anywhere. It seems like it should be declared as a member variable in the class.
private string lastResponse;
Everything else looks correct.
Thank you very much $$anonymous$$! This seems O$$anonymous$$, since the application is running error free now. However when I click on the button to which this script is attached, no reaction is noticeable. The facebook app is not started, and I do not notice that a screenshot is taken and tempting to upload to my timeline. $$anonymous$$y code is now as follows:
using UnityEngine;
using System.Collections;
using Facebook.Unity;
using Facebook.$$anonymous$$iniJSON;
public class FacebookShare : $$anonymous$$onoBehaviour {
private string lastResponse;
// Awake function from Unity's $$anonymous$$onoBehavior
void Awake()
{
if (!FB.IsInitialized)
{
// Initialize the Facebook SD$$anonymous$$
FB.Init(InitCallback, OnHideUnity);
}
else {
// Already initialized, signal an app activation App Event
FB.ActivateApp();
}
}
private void InitCallback()
{
if (FB.IsInitialized)
{
// Signal an app activation App Event
FB.ActivateApp();
// Continue with Facebook SD$$anonymous$$
// ...
}
else {
Debug.Log("Failed to Initialize the Facebook SD$$anonymous$$");
}
}
private void OnHideUnity(bool isGameShown)
{
if (!isGameShown)
{
// Pause the game - we will need to hide
Time.timeScale = 0;
}
else {
// Resume the game - we're getting focus again
Time.timeScale = 1;
}
}
public void ShareFb()
{
StartCoroutine(TakeScreenshot());
}
private IEnumerator TakeScreenshot()
{
yield return new WaitForEndOfFrame();
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();
var wwwForm = new WWWForm();
wwwForm.AddBinaryData("image", screenshot, "Screenshot.png");
FB.API("me/photos", Http$$anonymous$$ethod.POST, APICallback, wwwForm);
}
void APICallback(IGraphResult result)
{
if (result.Error != null)
lastResponse = "Error Response:\n" + result.Error;
else if (!FB.IsLoggedIn)
lastResponse = "Login cancelled by Player";
else
lastResponse = "Login was successful!";
}
}
Also I defined all the Andoid build Facebook settings within Unity.
Any Idea why there is no reaction when I click the button?
Hi All / @brianturner,
The code above still is not working. There is no reaction when I push the button. I see nothing happening. Has anyone an idea why it is not working?
I'm not sure why it wouldn't work. Is there anything in the logs reported by logcat? Does saving the screenshot to the gallery work?
Do you have the "publish_actions" permission properly authorized by Facebook.?