Home How to attach multiple videos and photos to a status post with Facebook graph API
Reply: 0

How to attach multiple videos and photos to a status post with Facebook graph API

Albert Vazquez
1#
Albert Vazquez Published in 2018-01-11 18:11:14Z

I'm trying to publish a post with multiple attached videos and photos via the Facebook Graph API. I uploaded the videos and photos first using a batch request and get the ids successfully for the uploaded media. Then I pass the media ids along with the post data using attached_media. Things work fine for single or multiple photos. But not for a single video or multiple videos. I got this error: "Graph returned an error: (#10) Application does not have permission for this action" whenever ids of videos are included in attached_media.

I know as a user you have the ability to attach multiple videos and photos to a Facebook post. Does the Facebook Graph API just flat out not support this for multiple videos?

I am working with C# and Unity

Here is the function that uploads videos and then sends a post with attached result videos ids:

private IEnumerator UploadMediaWithStatus(string message, string[] mediaUrls)
{
    FacebookPostStatusUpdatedEvent.Invoke("Submitting FB post ... ");

    var curPage = m_UserPages[FacebookFeedsList.GetInstance().ActiveFeed] as IDictionary<string, object>;
    var fbClient = new FacebookClient(curPage["access_token"].ToString());

    List<string> mediaIDs = new List<string> ();
    foreach (string mediaUrl in mediaUrls) 
    {
        WWW _media = new WWW("file://" + mediaUrl);
        yield return _media;            

        if (!System.String.IsNullOrEmpty(_media.error))
        {
            FacebookPostStatusUpdatedEvent.Invoke("FB post failed: error loading media " + mediaUrl);
            //TruLogger.LogError("cant load Image : " + _image.error);
            yield break;
        }

        byte[] mediaData = null;
        FacebookMediaObject medObj = new FacebookMediaObject();
        JsonObject p = new JsonObject();

        mediaData = _media.bytes;
        medObj.SetValue(mediaData);
        medObj.FileName = "InteractiveConsole.mp4";
        medObj.ContentType = "multipart/form-data";
        p.Add("description", message);
        p.Add("source", medObj);

        uploadedImgID = "";

        fbClient.PostCompleted +=  OnPostImageUploadComplete;
        fbClient.PostAsync("/me/videos", p);


        //wait for image upload status and hopefully it returned a media ID on successful image upload
        while (uploadedImgID == "") 
        {
            //if image updload failed because of rate limit failure we can just abort rest  of this
            if (rateLimitErrorOccurred) 
            {
                rateLimitErrorOccurred = false;
                yield break;
            }
            yield return true;
        }

        //if video uploaded succesfully
        if (uploadedImgID != null) 
        {
            mediaIDs.Add (uploadedImgID);

            var response = (JsonObject)fbClient.Get("/" + uploadedImgID + "?fields=status,published");
            TruLogger.LogError("Video Status Details: " + response.ToString());
            string vidStatus = (response["status"] as IDictionary<string, object>)["video_status"] as string;
            while( vidStatus != "ready")
            {
                yield return new WaitForSeconds(5.0f);
                response = (JsonObject)fbClient.Get("/" + uploadedImgID + "?fields=status,published");
                TruLogger.LogError("Video Status Details: " + response.ToString());
                vidStatus = (response["status"] as IDictionary<string, object>)["video_status"] as string;          
            }
            TruLogger.LogError("Video ready");
            yield return new WaitForSeconds(5.0f);
        }
    }

    if (mediaIDs.Count > 0) 
    {
        curPage = m_UserPages[FacebookFeedsList.GetInstance().ActiveFeed] as IDictionary<string, object>;
        fbClient = new FacebookClient(curPage["access_token"].ToString());
        JsonObject p = new JsonObject();
        p.Add("message", message);

        for (int i = 0; i < mediaIDs.Count; i++) 
        {
            p.Add ("attached_media["+ i + "]", "{\"media_fbid\":\"" + mediaIDs[i] + "\"}");
        }

        fbClient.PostCompleted += OnPostUploadComplete;
        fbClient.PostAsync("/me/feed", p);          
    }
}

void OnPostImageUploadComplete(object sender, FacebookApiEventArgs args)
{
    //if successful store resulting ID of image on Facebook
    try
    {
        var json = args.GetResultData<JsonObject>();
        uploadedImgID = json["id"].ToString();
        //TruLogger.LogError(json.ToString());
    }
    catch (Exception e)
    {
        apiPostStatusMessage = "FB post failed: media upload error";

        TruLogger.LogError (e.Message);
        TruLogger.LogError (e.InnerException.ToString());

        FacebookOAuthException oEx = (FacebookOAuthException)(e.InnerException);
        if (oEx != null) {
            TruLogger.LogError ("Error Type: " + oEx.ErrorType);
            TruLogger.LogError ("Error Code: " + oEx.ErrorCode);
            TruLogger.LogError ("Error Subcode: " + oEx.ErrorSubcode);  

            apiAbortErrorCode = oEx.ErrorCode;
            if (apiAbortErrorCode == 32 || apiAbortErrorCode == 4) 
            {
                rateLimitErrorOccurred = true;
                apiPostAbortErrorMessage = "WHOOPS! Looks like you have exceeded the daily API rate quota for one or more of your feeds. You may have to wait between 1-24 hours until Facebook replenishes your balance for these feeds: \n\n" + e.InnerException.ToString ();
            } 
            else 
            {
                rateLimitErrorOccurred = false;
                apiPostAbortErrorMessage = "WHOOPS! Looks like your session has expired or become invalid. Try Signing in again to revalidate your session: \n\n" + e.InnerException.ToString ();
            }
        } 
        else 
        {
            rateLimitErrorOccurred = false;
            apiAbortErrorCode = -1;
            apiPostAbortErrorMessage = "WHOOPS! Looks like your session has expired or become invalid. Try Signing in again to revalidate your session: \n\n" + e.InnerException.ToString ();
        }
        uploadedImgID = null;
    }
}

//generic call back for any post calls
void OnPostUploadComplete(object sender, FacebookApiEventArgs args)
{
    try
    {
        var json = args.GetResultData();
        TruLogger.LogError(json.ToString());

        apiPostStatusMessage = "FB post successfully submitted";
        //FacebookPostStatusUpdatedEvent.Invoke("FB post successfully submitted");
    }
    catch (Exception e)
    {
        apiPostStatusMessage = "FB post failed to submit";
        //FacebookPostStatusUpdatedEvent.Invoke("FB post failed to submit");

        TruLogger.LogError (e.Message);
        TruLogger.LogError (e.InnerException.ToString());

        FacebookOAuthException oEx = (FacebookOAuthException)(e.InnerException);
        if (oEx != null) {
            TruLogger.LogError ("Error Type: " + oEx.ErrorType);
            TruLogger.LogError ("Error Code: " + oEx.ErrorCode);
            TruLogger.LogError ("Error Subcode: " + oEx.ErrorSubcode);  

            apiAbortErrorCode = oEx.ErrorCode;
            if (apiAbortErrorCode == 32 || apiAbortErrorCode == 4) 
            {
                rateLimitErrorOccurred = true;
                apiPostAbortErrorMessage = "WHOOPS! Looks like you have exceeded the daily API rate quota for one or more of your feeds. You may have to wait between 1-24 hours until Facebook replenishes your balance for these feeds: \n\n" + e.InnerException.ToString ();
            } 
            else 
            {
                rateLimitErrorOccurred = false;
                apiPostAbortErrorMessage = "WHOOPS! Looks like your session has expired or become invalid. Try Signing in again to revalidate your session: \n\n" + e.InnerException.ToString ();
            }
        } 
        else 
        {
            rateLimitErrorOccurred = false;
            apiAbortErrorCode = -1;
            apiPostAbortErrorMessage = "WHOOPS! Looks like your session has expired or become invalid. Try Signing in again to revalidate your session: \n\n" + e.InnerException.ToString ();
        }
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311809 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO