如何使用Facebook图形API将多个视频和照片附加到状态帖子

时间:2018-01-11 18:11:15

标签: facebook facebook-graph-api video

我试图通过Facebook Graph API发布包含多个附加视频和照片的帖子。我首先使用批量请求上传视频和照片,并成功获取上传媒体的ID。然后我使用attached_media传递媒体ID和帖子数据。单张或多张照片的效果很好。但不适用于单个视频或多个视频。我收到此错误:" Graph返回错误:(#10)应用程序没有此操作的权限"只要视频的ID包含在attached_media中。

我知道作为用户,您可以将多个视频和照片附加到Facebook帖子。对于多个视频,Facebook Graph API是否不支持此功能?

我正在使用C#和Unity

以下是上传视频然后发送附有结果视频ID的帖子的功能:

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 ();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

Facebook 不允许在业务页面上直接上传多个视频或带有视频的照片。但是,您可以在个人页面上创建帖子,作为替代解决方案,您可以在个人页面上创建帖子并在业务页面上共享。

查看此视频了解更多信息:https://www.youtube.com/watch?v=AoK_1S71q1o