Search code examples
typescriptfacebook-graph-apifacebook-javascript-sdk

How do I upload a video to my Facebook page using the graph API?


I've been struggling with how to interpret the documentation obtained here and here. I'm using Facebook's Javascript SDK.

    async postVideo(page: IFacebookPage, videoFile: File): Promise<CreateAdResult> {
        return new Promise<CreateAdResult>(async (resolve, reject) => {
            try {
                const data = new FormData()
                data.append('source', videoFile)
                const result = await this.post(`/${page.id}/videos`, page, { source: data })
                resolve(result)
            } catch (error) { reject(error) }
        })
    }

    private async post(url: string, page: IFacebookPage, params: any): Promise<CreateAdResult> {
        return new Promise<CreateAdResult>(async (resolve, reject) => {
            const accessToken = await this.fetchFaceBookPageAccessToken(page.id)
            this.FB().api(url, "post", { ...params, access_token: accessToken }, (response: any) => {
                if (response && !response.error) {
                    resolve(CreateAdResult.PublishToFacebookSuccess)
                } else {
                    const msg = response && response.error ? `Failed to post content to Facebook page with error: ${response.error.message} ` : `Failed to post ad to Facebook page!`
                    reject(Error(msg)) // <----- Ends up here!!
                }
            })
        })
    }

All this just results in an error message on the form

Error: Failed to post content to Facebook page with error: There was a problem uploading your video file. Please try again.


Solution

  • I got around the problem by uploading the video somewhere else and provide the url to Facebook instead.

    I.e.

    async postVideo(page: IFacebookPage, videoUrl: string): Promise<CreateAdResult> {
        return new Promise<CreateAdResult>(async (resolve, reject) => {
            try {
                const result = await this.post(`/${page.id}/videos`, page, { file_url: videoUrl })
                resolve(result)
            } catch (error) { reject(error) }
        })
    }