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
OK I managed to get this working:
and
and
A URL like this should work:
Note the forward slashes, though you may get away with backslashes in the filename part.