skip to Main Content

I want to upload a photo from my local machine (e.g. a jpg) to a Facebook page I am the admin on, using the Facebook Graph API (using Access VBA).

I can post an image that is on the internet already to the Facebook page, with a message, that works fine.

But I want the file to come from my local machine…

I can’t work it out!

This code (VBA) works to upload a JPG that is already on the internet

    Dim httpRequest As Object
    Dim boundary As String
    Dim postData As String
    Dim pageID As String, accessToken As String, fileUrl As String, message As String
    pageID = "[My Page ID]"
    accessToken = "[My long-lived Access Token]"
    fileUrl = "https://www.facebook.com/images/fb_icon_325x325.png"
    message = "test message"

    boundary = "----------------------------" & Format(Now, "ddmmyyyyhhmmss")

    postData = "--" & boundary & vbCrLf & _
               "Content-Disposition: form-data; name=""message""" & vbCrLf & _
               "Content-Type: text/plain; charset=UTF-8" & vbCrLf & vbCrLf & _
               message & vbCrLf & _
               "--" & boundary & vbCrLf & _
               "Content-Disposition: form-data; name=""url""" & vbCrLf & _
               "Content-Type: text/plain; charset=UTF-8" & vbCrLf & vbCrLf & _
               fileUrl & vbCrLf & _
               "--" & boundary & "--"

    Set httpRequest = CreateObject("MSXML2.XMLHTTP")
    With httpRequest
        .Open "POST", "https://graph.facebook.com/" & pageID & "/photos?access_token=" & accessToken, False
        .setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
        .send (postData)

        If .status = 200 Then
            Debug.Print .responseText
        Else
            Debug.Print "Error: " & .status & " - " & .statusText
        End If
    End With

This works.

I can’t for the life of me work out how to change the photo to be one from my local machine though.

Please note: I have the correct token settings because, as I say, this code above works for a photo that is already on the internet – so my page ID and my access token must be correct.

[My text logical question will be…. how do I upload multiple photos?]

Thanks for any help you can offer!

2

Answers


  1. Chosen as BEST ANSWER

    OK I managed to get this working:

    Public Function UploadFileToFacebookPage()
    
        Dim httpRequest As Object
        Dim boundary As String
        Dim postData As String
        Dim pageID As String, accessToken As String, fileUrl As String, message As String
        pageID = "[My page ID]"
        accessToken = "[My long-lived access token]"
        
        Dim fileName As String, filePath As String
    
        filePath = "Z:Desktoptestimage.jpg"
        fileName = "testimage.jpg"
        message = "test message"
    
        boundary = "----------------------------" & Format(Now, "ddmmyyyyhhmmss")
               
        postData = "--" & boundary & vbCrLf
        postData = postData & "Content-Disposition: form-data; name=""message""" & vbCrLf
        postData = postData & "Content-Type: text/plain; charset=UTF-8" & vbCrLf & vbCrLf
        postData = postData & message & vbCrLf
    
        postData = postData & "--" & boundary & vbCrLf
        postData = postData & "Content-Disposition: form-data; name=""source""; filename=""" & fileName & """" & vbCrLf
        postData = postData & "Content-Type: image/jpeg" & vbCrLf & vbCrLf
        postData = postData & getBinaryFile(filePath) & vbCrLf
        postData = postData & "--" & boundary & "--" & vbCrLf
    
        Set httpRequest = CreateObject("MSXML2.XMLHTTP")
        With httpRequest
            .Open "POST", "https://graph.facebook.com/" & pageID & "/photos?access_token=" & accessToken, False
            .setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
            .send ToByteArray(postData) ' Convert from Unicode
            
    
            If .Status = 200 Then
                Debug.Print .ResponseText
            Else
                Debug.Print "Error: " & .Status & " - " & .statusText
            End If
        End With
    End Function
    

    and

    Function getBinaryFile(filePath) As Variant
      Dim binaryStream As Variant
      Set binaryStream = CreateObject("ADODB.Stream")
      binaryStream.Type = 1
      binaryStream.Open
      binaryStream.LoadFromFile filePath
      getBinaryFile = StrConv(binaryStream.Read, vbUnicode) ' Convert to Unicode
      binaryStream.Close
    End Function
    

    and

    Function ToByteArray(str As String) As Byte()
        ToByteArray = StrConv(str, vbFromUnicode)
    End Function
    

  2. A URL like this should work:

    file:///C:/Test/YourImage.jpg
    

    Note the forward slashes, though you may get away with backslashes in the filename part.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search