0

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!

  • Not finding VBA example but maybe you will get clue from https://stackoverflow.com/questions/58452122/how-to-upload-local-image-to-facebook or https://gist.github.com/nseo/2910244 – June7 Feb 07 '23 at 23:06
  • Sorry I've had a look at these and I just can't see how to make it work in VBA – Chris Lydon Feb 08 '23 at 21:28
  • This question shows code for converting image to stream https://stackoverflow.com/questions/38344715/excel-vba-get-adodb-stream-from-image-control-not-with-loadfromfile. Maybe relevent. Or this https://stackoverflow.com/questions/75391992/how-do-i-upload-jpg-to-my-own-webserver-using-vba-in-access – June7 Feb 08 '23 at 22:24

2 Answers2

0

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.

Gustav
  • 53,498
  • 7
  • 29
  • 55
0

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:\Desktop\testimage.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