How to post as Page using the Facebook SDK
I'm trying to post to a Facebook page AS the page using the Unity Facebook SDK running on iOS. As I understand, to do that, I need the pages access token with manage_pages
and publish_pages
. I know that I can get it from /me/accounts?fields=access_token
, but how do I tell AccessToken.CurrentAccessToken
to use my pages access token instead?
Right now i'm using the following:
var wwwForm = new WWWForm();
//wwwForm.AddField ("access_token", "A-T I NEED");
wwwForm.AddBinaryData("image", screenshot, "InteractiveConsole.png");
wwwForm.AddField("message", "herp derp. I did a thing! Did I do this right?");
FB.API("/PAGE-ID/photos", HttpMethod.POST, HandleResult, wwwForm);
I tried putting the access token manually, but that didn't work (so I commented it out).
With this as it is I'm getting an error, telling me that I need publish_actions
, wich is not correct since I'm not trying to post as the user. If I also get publish_actions
the post goes online, but is posted to the page as the user speaking. (User is also Admin)
Any Ideas ? Thanks!
Answer by PieterAlbers · Jun 13, 2017 at 02:02 PM
Did you solve this - I am can't get it to work properly either. Thanks, Pieter
I did. I answered this over at Stack Overflow:
https://stackoverflow.com/questions/43585245/changing-currentaccesstoken-in-facebook-unity-sdk
Do you happen to know how can I post it inside an Album, in here graphApi/page/photos it says the "aid" parameter is deprecated
or how can I even move it to an album. thanks to ur UnityWebRequest solution I could finally post as a page , but the photos are uploaded to the timeline album , cant change it anyhow.
Well, I don't have a lot time right now. But maybe this might help. On thing is to post to an Album by creating it, the other is getting the id of that album so you don't create a new album with the same name every time you upload to it. Probably not the best or easiest way to do it, but it works. Here you go:
// ALBU$$anonymous$$ NA$$anonymous$$E //
[System.Serializable]
public class AlbumInfo
{
public List<ActData> data;
}
[System.Serializable]
public class ActData
{
public string id;
public string name;
}
// ALBU$$anonymous$$ CALL //
public void AlbumCallback(IGraphResult result) {
inCallback = true;
if (result.Error != null)
{
album_data = result.RawResult;
inCallback = false;
}
else
{
var dataDict = result.RawResult as String;
Debug.Log(dataDict);
AlbumInfo ANON = JsonUtility.FromJson<AlbumInfo>(dataDict);
for(int i=0; i<ANON.data.Count; i++ ){
//Debug.Log(ANON.data [i].name);
if (ANON.data [i].name == AlbumName) {
AlbumID = ANON.data [i].id;
AlbumOnline = ANON.data [i].name;
Debug.Log("Das Album " + AlbumOnline + " hat die id " + AlbumID);
Album$$anonymous$$atch = true;
inCallback = false;
}
}
//Debug.Log (AlbumOnline);
}
}
IEnumerator UploadToPage(byte[] screenshot) {
#if UNITY_IPHONE
Handheld.SetActivityIndicatorStyle(UnityEngine.iOS.ActivityIndicatorStyle.WhiteLarge);
#endif
Handheld.StartActivityIndicator();
FB.API (PageID + "/albums?fields=name", Http$$anonymous$$ethod.GET, AlbumCallback);
while(inCallback)
yield return new WaitForSeconds(0.1f);
var wwwForm = new WWWForm ();
if (AlbumName != AlbumOnline || AlbumOnline == null) {
wwwForm.AddField ("name", AlbumName);
string url = "https" + "://graph.facebook.com/" + PageID + "/albums";
url += "?access_token=" + PageAccessToken;
using (UnityWebRequest www = UnityWebRequest.Post (url, wwwForm)) {
yield return www.Send ();
if (www.isError) {
Debug.Log (www.error);
IOSNativePopUp$$anonymous$$anager.show$$anonymous$$essage ("Failed!", "Session Reset");
ssCount = 0;
Image3Obj.SetActive (true);
ImagePicker.SetActive (false);
Handheld.StopActivityIndicator();
yield break;
} else {
Debug.Log ("Form upload complete!");
Debug.Log ("Created Album " + AlbumName);
// GET ID //
FB.API (PageID + "/albums?fields=name", Http$$anonymous$$ethod.GET, AlbumCallback);
}
}
}
while(inCallback)
yield return new WaitForSeconds(0.1f);
if (AlbumName == AlbumOnline && Album$$anonymous$$atch == true) {
wwwForm.AddField ("message", PostText);
wwwForm.AddBinaryData ("image", screenshot, "Tiger" + Time.time + ".png");
string url = "https" + "://graph.facebook.com/" + AlbumID + "/photos";
url += "?access_token=" + PageAccessToken;
using (UnityWebRequest www = UnityWebRequest.Post (url, wwwForm)) {
yield return www.Send ();
if (www.isError) {
Debug.Log (www.error);
IOSNativePopUp$$anonymous$$anager.show$$anonymous$$essage ("Failed!", "Session Reset");
ssCount = 0;
Image3Obj.SetActive (true);
ImagePicker.SetActive (false);
Handheld.StopActivityIndicator();
yield break;
} else {
Debug.Log ("Form upload complete!");
Debug.Log ("Uploaded Tiger" + Time.time + ".png");
IOSNativePopUp$$anonymous$$anager.show$$anonymous$$essage ("Success!", "Uploaded to " + PageName + " in Album \"" + AlbumName + "\"");
ssCount = 0;
Image3Obj.SetActive (true);
ImagePicker.SetActive (false);
Handheld.StopActivityIndicator();
yield break;
}
}
}
}
Ok thanks very much, I think I get it now, I used to do this => " {PageID}/{Album-Id}/photos?access_token=xxxx " and it gave me an Error, I have an album ID already. but will also give this a try , thanks a bunch =)
update: it worked =) I should've just used the album ID like a the page ID , and add the token field.
I've also acquired the page access token from the me/accounts using Get in FB.API , and split the RawResult from " and used the string that held the access token.
Your answer
![](https://koobas.hobune.stream/wayback/20220612120640im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get comments on Unity using Facebook Graph API 0 Answers
how to .get scores and set scores in facebook sdk 7.7 (Scores API) 1 Answer
Unity Facebook SDK Login in using FB app rather than Safari browser 1 Answer
Share to facebook, twitter,etc. Ios Please help out 0 Answers
How to use result.ResultDictionary to fetch full json of facebook user profile who logged in my app? 1 Answer