Facebook Graph API: How To Publish Text To Page, Including Image Attachment? - facebook-graph-api

The documentation gives an example of how to upload an image, not via url, but as an attachment:
https://developers.facebook.com/docs/messenger-platform/reference/attachment-upload-api
curl -F "message={'attachment':{'type':'image', 'payload':{'is_reusable':true}}}" -F "filedata=#square.png;type=image/png" "https://graph.facebook.com/v2.12/1843037429354999/photos?access_token=<ACCESS_TOKEN>"
This leads to the following request:
POST https://graph.facebook.com/v2.12/<MY_PAGE_ID>/photos?access_token=<MY_ACCESS_TOKEN> HTTP/1.1
Host: graph.facebook.com
User-Agent: curl/7.52.1
Accept: */*
Content-Length: 2833
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------c71046aadf1a4e33
--------------------------c71046aadf1a4e33
Content-Disposition: form-data; name="message"
{'attachment':{'type':'image', 'payload':{'is_reusable':true}}}
--------------------------c71046aadf1a4e33
Content-Disposition: form-data; name="filedata"; filename="square.png"
Content-Type: image/png
<IMAGE_DATA>
--------------------------c71046aadf1a4e33--
However, this example does not work as advertised. The json object in the message parameter is literally posted as text in the post.
I'd like to be able to send along json post data that includes a 'message' and a 'access_token', so that I don't have to include the access token in the url.
How can I ensure that the type/image/payload json object is applied as a configuration, rather than used as the literal post message?
And how can I send along 'message' and 'access_token' in the postdata?

Found the solution.
This command...
curl -F "access_token=<MY_ACCESS_TOKEN>" -F "message=This is the message I wanna post." -F "filedata=#square.png;type=image/png" "https://graph.facebook.com/v2.12/<MY_PAGE_ID>/photos"
Gives this working request:
POST https://graph.facebook.com/v2.12/<MY_PAGE_ID>/photos HTTP/1.1
Host: graph.facebook.com
User-Agent: curl/7.52.1
Accept: */*
Content-Length: 3077
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------e94f377a15b0500f
--------------------------e94f377a15b0500f
Content-Disposition: form-data; name="access_token"
<MY_ACCESS_TOKEN>
--------------------------e94f377a15b0500f
Content-Disposition: form-data; name="message"
This is the message I wanna post.
--------------------------e94f377a15b0500f
Content-Disposition: form-data; name="filedata"; filename="square.png"
Content-Type: image/png
<IMAGE_DATA>
--------------------------e94f377a15b0500f--
All of a sudden it dawned on me. If the 'message' parameter posts a message, then why not simply put the message I want to post in there. Might as well include 'access_token' then, too. And it worked.
Instead of the deprecated 'message', you can also use 'caption' and it will also work.
Anyway, I can now attempt to reproduce this http request from C#. Glad I found this, because it saves me the effort from having to install the Facebook PHP SDK just to see how such a request should be formed.
(Why is there so little Facebook Graph API C# support, anyway?)
I still have no idea how to properly send along the attachment/type/image/payload json object from Facebook's documentation, though.
Oh, well.

Related

Translate informatica Documentation into REST API Call (Postman)

I am working on importing data into Informatica Reference360 and struggling to interpret documentation to construct an API call in POSTMAN.
Here is the example they are giving
POST https://use4-mdm.dm-us.informaticacloud.com/rdm-service/external/v1/import HTTP/1.1
Content-Type: multipart/form-data; boundary=6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
IDS-SESSION-ID: XXXXXXXXXXXXXXXXXXXXXX
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=file; filename=import-code-values.csv
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=importSettings
Content-Type: application/json;charset=UTF-8
{
"delimiter":"COMMA",
"textQualifier":"DOUBLE_QUOTE",
"codepage":"UTF8",
"dateFormat":"ISO",
"containerType":"CODELIST",
"containerId":"9ab3201990a54dcdc86f54cf",
"startingRow":null
}
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm--
The parts I do not understand:
what is this for: boundary=6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm.Should I put it in header as well?
What is this and where should I use it (are these two alternatives and only one should be used)?
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=file; filename=import-code-values.csv
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=importSettings
Content-Type: application/json;charset=UTF-8
...
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm--
How should I pass the actual file for import, should it be in body (form-data)?
If so which parts of the API payload should be in raw and which in form-data?
The link to documentation
Basically, your POST request requires 2 parts:
1.Upload file csv
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=file; filename=import-code-values.csv
2.Json object
--6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm
Content-Disposition: form-data; name=importSettings
Content-Type: application/json;charset=UTF-8
You can check the similar question here:
How to upload a file and JSON data in Postman?
Answering my own questions:
what is this for: boundary=6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm.Should I put it in header as well?
this is generated automatically in the header by postman
What is this and where should I use it (are these two alternatives and only one should be used)?
both of them should
If so which parts of the API payload should be in raw and which in form-data?
everything should be input in body - form data

Attachment sent via AWS SES not visible on iphone

Our service send email with attachment which is not visible on ios devices.
AWS team suggested to add double quotes around file name but when i try to add ", MimeBodyPart turning into escape character.
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDisposition(Part.ATTACHMENT);
messageBodyPart.setDataHandler(new DataHandler(Base64.getMimeDecoder()
.decode(attachment.getAttachment()), attachment.getContentType()));
messageBodyPart.setFileName("\""+ attachment.getName()+"\"");
From: donotreply#abc.com To: test#abc.com Message-ID:
<962944318.2.1571250351443#[10.200.78.179]> Subject: subject for test
MIME-Version: 1.0 Content-Type: multipart/related;
boundary="----=_Part_1_977674685.1571250351337"
------=_Part_1_977674685.1571250351337 Content-Type: multipart/alternative;
boundary="----=_Part_0_474488818.1571250351334"
------=_Part_0_474488818.1571250351334 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit
Hi your email send is success
------=_Part_0_474488818.1571250351334--
------=_Part_1_977674685.1571250351337 Content-Type: application/pdf; name="\"myData.pdf\"" Content-Transfer-Encoding:
base64 Content-Disposition: attachment; filename="\"myData.pdf\""
ICAgICAgICAgDXhyZWYNNCAxMQ0wMDAwMDAwMDE2IDAwMDAwIG4NCjAwMDAwMDA2NjAgMDAwMDAg
------=_Part_1_977674685.1571250351337--
Found fix, issue was with respect to content-type. changing content-type to multipart/mixed resolved it.

Restsharp vs Postman - content-type for a video

For testing one of our API endpoints, I need to upload a video. Our testing framework uses RestSharp.
The call works with Postman, which generates the following relevant headers and body:
Content-Type: multipart/form-data; boundary=--------------------------285414664033564173408812
Accept: */*
content-length: 1055942
----------------------------285414664033564173408812
Content-Disposition: form-data; name=""; filename="uservideo.mp4"
Content-Type: video/mp4
// binary data here
Now, when attempting the same using RestSharp, the request is constructed as follows but it fails:
Accept: application/json
Content-Type: multipart/form-data; boundary=-----------------------------28947758029299
Content-Length: 1055956
-------------------------------28947758029299
Content-Disposition: form-data; name="uservideoTest"; filename="uservideo.mp4"
Content-Type: application/octet-stream
// binary data here
The code used is as follows:
restRequest.AlwaysMultipartFormData = true;
restRequest.AddFile(request.FileName, request.FullPath);
Is it possible to have the RestSharp request constructed like the Postman request?
Found it, the answer is:
restRequest.AlwaysMultipartFormData = true;
restRequest.AddFile(request.FileName, request.FullPath, "video/mp4");

Upload file to Drive and set file name

I want to upload a file to Google Drive and set its name as I understood I have to use uploadType=multipart
I am under c++ and using cURL lib.
How can I proceed?
You need to make a multipart request with metadata and media parts. Use curl_formadd and make sure your request looks like:
POST /upload/drive/v2/files?uploadType=multipart HTTP/1.1
Host: www.googleapis.com
Authorization: your_auth_token
Content-Type: multipart/related; boundary="boundary_tag"
--boundary_tag
Content-Type: application/json; charset=UTF-8
{
"title": "My title"
}
--boundary_tag
Content-Type: image/jpeg
data
--boundary_tag--

Django CSRF failure on ajax post requests on Opera only

Ok so AJAX POST requests work fine in Mozilla and Chromium, but fail in Opera. I get the standard CSRF error (403). I tried different versions of Opera and they failed in every one I tried. Btw, I'm using the jquery/django snippet that sets X-CSRFToken in the header, as verified in the "bad Opera request" below.
I made a view in a different project that was very simple and ajax post requests worked fine in Opera. I looked at the request details and see differences. The good request doesn't set any weird X-Opera-Info and other opera params even thought I'm using the same browser. If this is the issue, is there a way to remove those extra params? Or does anyone have any other advice or ideas on what the issue may be? I know it's not my view function because I tried just returning an HttpResponse immediately and even that gets 403'd. Thanks a million guys.
####################
OPERA GOOD REQUEST
##############
Request details
POST /test HTTP/1.1
User-Agent: Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.00
Host: 127.0.0.1:8000
Accept-Language: en-US,en;q=0.9
Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1
Accept-Encoding: deflate, gzip, x-gzip, identity, *;q=0
Referer: http://127.0.0.1:8000/test
Cookie: csrftoken=1c6441404c991f7ae3b6d7d49f91f280
Cookie2: $Version=1
Connection: Keep-Alive, TE
TE: deflate, gzip, chunked, identity, trailers
Content-Length: 6
Content-Type: application/x-www-form-urlencoded
Accept: */*
X-CSRFToken: 1c6441404c991f7ae3b6d7d49f91f280
X-Requested-With: XMLHttpRequest
Content-Transfer-Encoding: binary
###################
OPERA BAD REQUEST
####################
Request details
POST http://facebook.example.com/remove-person HTTP/1.1
User-Agent: Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.00
Host: facebook.example.com
Accept-Language: en-US,en;q=0.9
Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1
Accept-Encoding: deflate, gzip, x-gzip, identity, *;q=0
Referer: http://facebook.example.com/
Cookie: signed_request=5-f0_7pZLILrp6MLocsdMoNYAaZr-wCnU2cPbLC1bZg.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEzMDg4MTYwMDAsImlzc3VlZF9hdCI6MTMwODgxMTQyNywib2F1dGhfdG9rZW4iOiIyMjMyNDY5NDEwMjc3MTR8Mi5BUURVRGM2ZFFLSElnN1h3LjM2MDAuMTMwODgxNjAwMC4xLTU0MDIwMjZ8b2QtX1diNTh3aG1wTnNHYUh4cTNtOVBpWkswIiwidXNlciI6eyJjb3VudHJ5IjoidXMiLCJsb2NhbGUiOiJlbl9VUyIsImFnZSI6eyJtaW4iOjIxfX0sInVzZXJfaWQiOiI1NDAyMDI2In0; csrftoken=d4cdc6a75ed264d295a410dd98982c42; fbs_223246941027714="access_token=223246941027714%7C2.AQBlhzavZjzd8c7J.3600.1308819600.1-5402026%7CdsD6VESpGJb3m0EdD1mhFZtDI24&base_domain=example.com&expires=1308819600&secret=QaTNS988wl0FU6A0LG9qDQ__&session_key=2.AQBlhzavZjzd8c7J.3600.1308819600.1-5402026&sig=61e7e13091501f35793d3cda8c20835b&uid=5402026"
Cookie2: $Version=1
Connection: Keep-Alive, TE
TE: deflate, gzip, chunked, identity, trailers
Content-Length: 14
X-Opera-Info: ID=448, p=4, f=15, sw=1440, sh=900
X-Opera-ID: e79c37b56a58510d26b56882453bddb6d2c2dae858129139113f6346ea23ca6b
X-Opera-Host: r18-02:12420
X-OA: 1322 b5834cb13259fbd50b87b576b5e8b9a8bcc1384478c2ea79cc65614dc1b67c27
X-OB: evenes
Content-Type: application/x-www-form-urlencoded
Accept: */*
X-CSRFToken: d4cdc6a75ed264d295a410dd98982c42
X-Requested-With: XMLHttpRequest
Content-Transfer-Encoding: binary
I recently ran into a similar problem. I was trying to do, for the first time, a post from AJAX on a view where Django wasn't sending a CSRF cookie. For reasons I can't explain, this was working on all browsers I tried except Opera.
This scenario is described in the Django docs, and they suggest using the ensure_csrf_cookie decorator.
Another thing you can do if it is too burdensome to wrap all the potential views with that decorator is to add something like this to your base template:
<script>
var csrf_token = "{{ csrf_token }}";
</script>
And then whenever you do an AJAX post, always include the key pair csrfmiddlewaretoken: csrf_token with your POST data.
Once I did the above, my posts with Opera started working.
Does it work if you disable Opera Turbo? These extra headers seem to be Turbo-related and perhaps this makes some kind of difference.
X-CSRFToken: {{ csrf_token }}}
value should be send from client in AJAX request HTTP headers to be recognized by Django