Facebook Unity API - post screenshot with link and description? - facebook

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?

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

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.

Follow these steps:
First login using FB.LogInWithPublishPermissions by adding the "publish_actions" permission in parameters.
Use Facebook Graph API to upload the image.
For more details link is here.

Related

Get profile Facebook photo without access token

I am trying to get users profile photo using facebook graph
I thought photos were public and you do not need to have an acces token but when trying I receive A question mark photo.
Trying with my Id
http://graph.facebook.com/10150172705012177/picture
returns a blue question mark, by the graph explorer I get access token and I Have the photo
http://graph.facebook.com/10150172705012177/picture?access_token=xxxxxxx
Can I get the profile photo with the fb id without access token???
With:
FB.API("10150172705012177/picture", HttpMethod.GET, delegate(IGraphResult photoResult)
and With
public static IEnumerator GetFBProfilePicture()
{
WWW url = new WWW(System.Uri.EscapeUriString("https: //graph.facebook.com/" + 10150172705012177 + "/picture?type=large"));
yield return url;
Debug.Log("Completed.");
Texture2D texture = new Texture2D(180, 180, TextureFormat.DXT1, false);
url.LoadImageIntoTexture(texture);
// ...
var image = Canvas.FindObjectOfType<Image>();
image.overrideSprite = Sprite.Create(texture, new Rect(0, 0, 8, 8), new Vector2(0.5f, 0.5f)); ;
}
I receive the same, a red question mark
Resolved!!
I just was getting the id photo and not my Facebook Id
With my facebook Id works :D

how to share to facebook without showing feed dialog in android application?

I tried a lot about sharing to facebook without showing dialog using facebook android sdk,but could not get solution. Please help me to share to facebook without dialog.
Use GraphReqest. This code posts a Bitmap image on facebook with caption using facebook sdk 4.2
Bitmap bmp;
String caption;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
final JSONObject graphObject=new JSONObject();
Callback callback=new Callback() {
#Override
public void onCompleted(GraphResponse response) {
// your code
}
};
GraphRequest request=GraphRequest.newPostRequest(AccessToken.getCurrentAccessToken(), "me/photos", graphObject, callback);
Bundle params = new Bundle();
//params.putString("method", "photos.upload");
params.putString("caption", caption);
params.putByteArray("picture", byteArray);
request.setParameters(params);
request.executeAsync();
You will have to take publish_actions permissions from the user
LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions"));
Moreover, If your app requires publish_actions, then you also need to send your app for the review on fb developers page, otherwise it will only work for developer accounts and tester accounts.

Cannot Update Facebook page app custom_image

I am trying to update my page app image with graph api but nothing happens. i have right access token , i could change the custom_name but not custom_image.
var fbdc = new Dictionary<string, object>();
fbdc.Add("access_token", AccessToken);
fbdc.Add("custom_name", Name);
fbdc.Add("custom_image",Image);
result = fb.Post(tab.id, fbdc);
I get the result true from graph api but still the image doesnt change.My image is 111*74.jpg. i was able to upload the same image manually but not through graph api.
Am i doing some thing wrong ?
var fbparams = new Dictionary<string, object>();
string path = #"c:\test.jpg";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
var picture = new FacebookMediaObject
{
//Tell facebook that we are sending image
ContentType = "image/jpeg",
//Give name to the image
FileName = "test"
};
//Create a new byteArray with right length
var img = tabImageInfo.Media;
//Convert the image content into bytearray
fs.Read(img, 0, img.Length);
//Close the stream
fs.Close();
//Put the bytearray into Picture
picture.SetValue(img);
//Add the image into parameters
fbparams.Add("custom_image", picture);
fb.Post(tab.id, fbparams);

facebook 3.5sdk open graph Explicitly Share

I'm trying to get my OpenGraph action to the news feed, and I learned that for that I need to add the ExplicitlyShared Capability to the Facebook app's OpenGraph action settings, and I did. Yet when I put it in my code, like this:
OpenGraphObject model = OpenGraphObject.Factory.createForPost("origame_app:model");
model.setProperty("title", modelname);
model.setProperty("url", "http://samples.ogp.me/1386546688246429");
model.setProperty("description", modeldesc);
Bitmap bitmap = decodeSampledBitmapFromUri(filepath[position], 500, 500);
List<Bitmap> images = new ArrayList<Bitmap>();
images.add(bitmap);
OpenGraphAction action = GraphObject.Factory.create(OpenGraphAction.class);
action.setProperty("model", model);
action.setExplicitlyShared(true);
#SuppressWarnings("deprecation")
FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(this, action, "origame_app:fold", "model")
.setImageAttachmentsForObject("model", images, true)
.build();
uiHelper.trackPendingDialogCall(shareDialog.present());
I get an error. But otherwise the OpenGraph works great. What's the problem?
I was also dealing with this issue and finally, I found a workaround. Instead of calling
action.setExplicitlyShared(true);
use
action.setProperty("explicitly_shared", true);
I reported this bug to Facebook: https://developers.facebook.com/bugs/559486710849474/.

Facebook change image in page tab

I'm trying to change a facebook page tab image programatically. The idea is when my app will finish the instalation process, it will be change the image of the tab where it place it. After finish the process and give permissions to the app, The response of fb is "unautorized"
{"error":{"message":"(#300) Edit failure","type":"OAuthException","code":300}}
I searched about this error, the most close aproach was: https://developers.facebook.com/bugs/255313014574414/.
I tried the same CURL example described in the error and have the same response.
The strange thing is, when i tried change the image inside fb, it fails too. I think the problem is when the app ask permissions to install in the user page, and don't have enough permits.
But i don't know hot i chage the permit to authorize chage the image of the pagetab.
var fbparams = new Dictionary<string, object>();
string path = #"c:\test.jpg";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
var picture = new FacebookMediaObject
{
//Tell facebook that we are sending image
ContentType = "image/jpeg",
//Give name to the image
FileName = "test"
};
//Create a new byteArray with right length
var img = tabImageInfo.Media;
//Convert the image content into bytearray
fs.Read(img, 0, img.Length);`enter code here`
//Close the stream
fs.Close();
//Put the bytearray into Picture
picture.SetValue(img);
//Add the image into parameters
fbparams.Add("custom_image", picture);
fb.Post(tab.id, fbparams);