Changing CurrentAccessToken in Facebook Unity SDK

2019-09-18 16:31发布

问题:

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!

回答1:

So, I filed a bug report to facebook and as it turns out: "… at this time this functionality is not supported." Wich simply means there is now way to use the Page Access Token you acquired via the FB.API within the FB.API. And they are not going to tell you abot it in the documentation.

As a workaround I simply use a UnityWebRequest like this:

IEnumerator UploadToPage(byte[] screenshot) {

    var wwwForm = new WWWForm();
    wwwForm.AddField("message", "herp derp.  I did a thing!  Did I do this right?");
    wwwForm.AddBinaryData("image", screenshot, "Test.png");

    string url = "https" + "://graph.facebook.com/"+ PageID + "/photos";
    url += "?access_token=" + PageAccessToken;

    using (UnityWebRequest www = UnityWebRequest.Post(url, wwwForm))

    {
        yield return www.Send();

        if (www.isError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
    }

    Debug.Log(url);
}