public/Invoke-InstagramSingleMediaPost.ps1

function Invoke-InstagramSingleMediaPost {
    <#
        .EXAMPLE
            Invoke-InstagramSingleMediaPost -PageId $PageId -Token $Token -Caption $Template -BucketName $BucketName -File $S3Key
 
        .EXAMPLE
            Invoke-InstagramSingleMediaPost -PageId $PageId -Token $Token -BucketName $BucketName -File $S3Key -Story
 
        .LINK
            https://developers.facebook.com/docs/instagram-platform/instagram-api-with-facebook-login/content-publishing
         
        .LINK
            https://www.simpleimageresizer.com/resize-image-for-instagram
 
        .LINK
            https://png2jpg.com/
         
    #>

    [CmdletBinding()]
    param(

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$BucketName,

        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [string]$Caption,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$File,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$PageId,

        [Parameter(Mandatory=$false)]
        [switch]$Story,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Token
        
    )
    process{

        $ErrorActionPreference = 'Stop'

        try {
    
            $PathExists = Test-Path -Path $Path

            if ($PathExists) {

                Start-Sleep -Seconds 10
                
                $Form1 = @{
                    access_token = $Token
                    caption = $Caption
                    image_url = "https://$BucketName.s3.eu-west-1.amazonaws.com/$File"
                }

                if ($Story) {

                    $Form1 = @{
                        access_token = $Token
                        media_type = 'STORIES'
                        image_url = "https://$BucketName.s3.eu-west-1.amazonaws.com/$File"
                    }
    
                } # if
                
                $Uri = "https://graph.facebook.com/v22.0/$PageId/media"
                $Response = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form1 -ErrorAction Stop
                
                $Form2 = @{
                    access_token = $Token
                    creation_id = $($Response.Id)
                }
                
                $Uri = "https://graph.facebook.com/v22.0/$PageId/media_publish"
                Invoke-RestMethod -Uri $Uri -Method Post -Form $Form2 -ErrorAction Stop
                
            } # if

        }
        catch {

             throw "$($MyInvocation.MyCommand.Name): $_.Exception.Message"
 
         } # trycatch

    } # process

} # function