当前位置: 首页 > 知识库问答 >
问题:

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

欧阳斌
2023-03-14

下面是一个上传视频然后发送附带结果视频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个答案

汪同
2023-03-14

Facebook不允许在商业页面上直接上传多个视频或带有视频的照片。但是,在个人页面上也是可能的,因此作为一种替代解决方案,您可以在个人页面上创建一篇文章,并在业务页面上共享它。

 类似资料:
  • 从post_id=each_post_id的流中选择附件 考虑到我将不得不发送这个请求每一个帖子和FQL将很快消失,我正在寻找一个替代方案。

  • 用消息或图像发布定期帖子对我来说很好,但没有视频 下面是我的代码: 视频和我的js运行文件在同一个文件夹里。

  • 了解如何将 Apple 照片图库迁移到 Lightroom - Adobe 的全新一体化服务,专为摄影爱好者设计。 准备要进行迁移的 Apple 照片图库 请确保将您要迁移的 Apple 照片图库设置为“系统照片图库”。要将图库设置为“系统照片图库”,请执行以下步骤: 打开 Apple 照片,并在菜单栏中选择偏好设置。 单击通用标签,然后在图库位置下方选择用作系统照片图库。 选择“用作系统照片图库

  • 下面是我的GridView的源代码:

  • 我的应用程序(使用SDK小于24)可以使用相机拍照和视频。这些照片和视频可以在应用程序外的图库中查看。SDK 24及以上版本要求FileProvider创建uri,以便将照片或视频保存到多媒体资料中。 在SDK 24之前,我会使用uri和拍照意图: 使用SDK24及更高版本会出现异常: 我想使用FileProvider实现同样的目标(没有例外)。下面的实现可以拍照,但照片不会出现在图库中。我想知道

  • 我有一个Facebook页面,我正在尝试上传一个已经托管在我的服务器上的视频。我需要通过javascript来做到这一点,我所拥有的只是视频的src链接(类似于 )。作为管理员,我拥有所有必需的权限()。 facebook api说我必须以的形式发送数据,这意味着视频内容。为了通过ajax实现这一点,我在stackoverflow上回答了这个问题,并学习了如何在jQuery中使用ajax请求发送F