Facebook Graph API Messenger integration - The parameter recipient is required - facebook

I'm trying to create a bot which interacts with Facebook Messenger. I've set up my webhook and can receive messages coming from Facebook. However, when I try to send a message, I get the following error back from Facebook:
{"error":{"message":"(#100) The parameter recipient is required","type":"OAuthException","code":100,"fbtrace_id":"F3iVNecj10i"}}
However, I've definitely got the recipient ID in my request. I've sent the request with my bot, cURL and the Chrome Poster extension and get the same result each time. The JSON I send is:
{"recipient":{"id":"XXXXXXXXXXXXXX"},"message":{"text":"hello, world!"}}
When using cURL, I took the example directly from the Facebook documentation and send this:
curl -k -X POST -H "Content-Type: application/json" -d '{"recipient":{"id":"XXXXXXXXXXXXXXXX"},"message":{"text":"hello, world!"}}' "https://graph.facebook.com/v2.6/me/messages?access_token=ACCESS_TOKEN"
The only difference between this and the example on Facebook is the -k which stops cURL from checking the SSL certificate. I'm tunneling through to my app using ngrok for the incoming messages but sending my requests direct to the Facebook Graph API. The fact that it's happening in my app, cURL and Chrome Poster makes me think that it's something to do with the request (but I can't see what) or my Facebook app setup. Any help is greatly appreciated.

Turns out there were a few issues. The cURL request didn't include the quotes in the JSON so the quotes had to be escaped with \ characters. The Chrome Poster request didn't work because "content-type: application/json" wasn't set in the header. And my webapp didn't work because the JSON had a ";" at the end of it.
So, the Facebook message was an indication of poorly formatted JSON, just not a very direct one!

Check that the JSON payload is well formed.
I used the Postman.app to help me out with this — it's also available on Windows.
Steps
Copy the URL into the "Enter request URL field". This would include the access_token
Change the HTTP verb to GET
Under the "Headers" header, set Content-Type to application/json
Under the "Body" header, select "raw" and paste your JSON payload there. Make sure that this JSON payload is well formed by watching the error indicator displayed beside the line numbers.
Once I got this fixed, I was able to move on to the next step.

I got similar error some time back. Try using Postman. I tried the same request and replaced the user id and the page access token. It works fine.
Click on the Import button on the top and paste your curl request under raw. Then try running the call. If you get the same error, go to the body and modify it. Make sure you put this in the body part of the Postman request. Replace the recipient id with yours.
{
"recipient":
{
"id":"123456789"
},
"message":
{
"text":"hello, world!"
}
}
This is the full cURL call : Change Recipient ID and Page Access Token
curl -X POST -H "Content-Type: application/json" -d '{ "recipient":{"id":"1234567" }, "message":{ "text":"hello from bot" }}' "https://graph.facebook.com/v2.6/me/messages?access_token=PASTETHETOKENHERE"

Related

Understanding the GET/POST/DELETE requests on a basic level?

I'm currently learning to use REST API (from WooCommerce in this case) and got some basic questions:
How to see complete request string in Postman software?
I'm testing a simple GET request which works great with for example:
<host>/wp-json/wc/v3/products
to receive the product list. In this case I use the authorization tab to enter my user/pass as Basic Auth.
I also tested curl.exe using another simple Windows command prompt. This also returned product list:
curl.exe <host>/wp-json/wc/v3/products -u mykey:mysecret
What is the difference between them? The last example is a simple GET, i assume, although it's not stated. How about POST or DELETE etc? This is what i don't understand: A https request can only have an address and eventual parameters. Where and how does "GET" come into the picture?!
If possible, I would like the see the complete URL request (as one string) from the working Postman example?
My last question is about testing the same method on another server/service which is not WooCommerce. Afaik this service is created with something called swagger:
curl "<host>/orderapi/item" -H "accept: application/json" -H "X-Customer: <customer>" -H "X-ApiKey: <mykey>" -H "X-ApiSecret: <mysecret>" -H "Content-Type: application/json"
This also returns a list of, in this case orders instead of products. All good.
But for this example I haven't figured out how to achieve the same request in Postman. What auth method should I use?
And again, I don't understand the GET/POST/DELETE thing. And I also would like to see the complete request as one-string.
1) How to see complete request string in Postman software? I would like the see the complete URL request (as one string) from the working Postman example
On version 9.x.x:
The code window(image) shows the choosen method (yellow mark) and the code window(red arrow), where you get the actual
curl code(image)
2) What is the difference between them? The last example is a simple GET, i assume, although it's not stated. How about POST or DELETE etc? Where and how does "GET" come into the picture?
From the curl documentation:
-X, --request
(HTTP) Specifies a custom request method to use when communicating
with the HTTP server. The specified request method will be used
instead of the method otherwise used (which defaults to GET). Read the
HTTP 1.1 specification for details and explanations. Common additional
HTTP requests include PUT and DELETE, but related technologies like
WebDAV offers PROPFIND, COPY, MOVE and more.
GET is the default method for curl, which means:
curl.exe <host>/wp-json/wc/v3/products -u mykey:mysecret
is the same as:
curl.exe <host>/wp-json/wc/v3/products -u mykey:mysecret -X "GET"
so, for a POST/DELETE/... you should change your '-X' parameter for example:
curl.exe <host>/wp-json/wc/v3/products -u mykey:mysecret -X "POST" [...otherOptions]
(Assuming that you can receive a POST on the url above)
3) [On another server/service] I haven't figured out how to achieve the same request in Postman. What auth method should I use?
The -H specify the header parameter you are passing. You have those in your example:
accept: application/json
X-Customer:
X-ApiKey:
X-ApiSecret:
Content-Type: application/json
You need to add those in your postman on the headers(image) tab. In this case you don't need to specify a auth method, once you're sending the ApiKey on the header. In addition to that, you can specify the authorization Type to be "Api Key" and put X-ApiKey as key and your apikey value on the value field(image). It'll generate the same request as shown in the headers image.
curl, at least the GNU one on Linux, uses GET method by default. If you want to change a HTTP method in your request, there's -X option, for example:
$ curl -X DELETE https://example.com
Postman has something called Postman Console which you can open by pressing Alt + Ctrl + C:
and where you can see more details about requests and responses.
Postman also lets you import curl commands, so you don't need to manually prepare the request, you can only paste the curl command in Postman.
There are many resources online on the specifics, e.g. how to import a curl command.

Getting "403" error while registering an Outgoing Webhook via PostMan

URL :
https://circuitsandbox.net/rest/v2/webhooks
My Headers :
Content-Type : application/x-www-form-urlencoded
Authorization : Bearer ot-xxxxxxxxxxxx
Body :
url - Some URl
filter - CONVERSATION.CREATE
Error I am getting :
"The permission to access this resource is not granted. Scopes ::= [ALL, READ_CONVERSATIONS, READ_USER]"
Plus If i want to send extra filters thn will it be comma separated values?
If you are getting a 403, I would suspect a scope error (as mentioned by Roger) or an authentication problem.
For the first, please show us which scopes are currently selected for the application ; for authentication, can you check if you can make other API calls successfully ?
Here is what it looks like in Postman
Make sure your app registration contains the scopes that your app is asking for. For a simple outgoing webhook registration you would only need the scope READ_CONVERSATIONS.
See https://github.com/circuit/circuit-REST-bot/blob/master/app.js for an example on how to register for a webhook. This example registers for CONVERSATION.ADD, but CONVERSATION.CREATE is very similar.
If you still have problems please post a code example, or even a link to an app on repl.it.
Here is an example HTTP request to register the webhook. Note that the body is sent as text/plain (which is the default and its header can be omitted). Also note that the callback url is http. https is not yet supported.
POST https://circuitsandbox.net/rest/v2/webhooks HTTP/1.1
Host: circuitsandbox.net
Content-Type: text/plain
Authorization: Bearer <token>
url=http://90587c6d.ngrok.io/webhook&filter=CONVERSATION.CREATE
and here is a curl command
curl -X POST https://circuitsandbox.net/rest/v2/webhooks -H "Authorization: Bearer <token>" -d "url=http://90587c6d.ngrok.io/webhook&filter=CONVERSATION.CREATE"

Query string (uri syntax) for a RESTFul API request

I'm very new to REST and google cloud endpoints. I've followed the tutorial
Getting Started with Endpoints Frameworks on App Engine and I've executed the API query as stated in the tutorial successfully:
curl --header "Content-Type: application/json" --request POST --data '{"message":"hello world"}' http://localhost:8080/_ah/api/echo/v1/echo
But I didn't manage it to find the corresponding URI query statement to be used in the browser.
I tried
http://localhost:8080/_ah/api/echo/v1/echo?{"message":"hello world"}
http://localhost:8080/_ah/api/echo/v1/echo?=message="hello world"
and a lot of combinations without success and which resulted in receiving no response.
How does the URI statement, corresponding to the cURL request as stated above, look like?
Thank you in advance.
BTW: This is the endpoint implementation of the API method "echo":
#ApiMethod(name = "echo")
public Message echoPathParameter(Message message, #Named("n") int n) {
return doEcho(message, n);
}
From the man page of curl:
-d/--data
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.
application/x-www-form-urlencoded means that the body of the request contains
message=hello+world

Drupal 8 API REST login works on curl but not postman

I have a bizarre issue with Drupal's REST API that I can't work out.
The exact same POST to /user/login?_format=json does NOT work in Postman (error 403 Forbidden) but with same parameters it works correctly in CURL.
This Curl works:
curl --header "Content-type: application/json" --request POST --data '{"name":"bobo", "pass":"xxx"}' http://[domain]/user/login?_format=json
I get back json with the uid, name, a csrf token, logout token and any roles.
Now in Postman, I select Post, my url is "http://[domain]/user/login?_format=json" for header i select 'Content-Type' and value 'application/json' and paste the {"name":"bobo", "pass":"xxx"} section into 'raw' under body.
Now I get 403 Forbidden and response: {"message":""}
Is there a setting in Postman I'm missing or is this a bug somewhere?? It seems utterly bizarre to me.
Actually, all is correct here.
User should be logged out to access /user/login endpoint (check user.routing.yml). Unlike curl, Postman behaves as a browser, i.e. saves all session cookies, so you are just logged in after first call and can't anymore access login endpoint. You may call /user/logout after logging in to be able to call it again.
Thus, I think your code works properly :)

How to post credentials using POSTMAN client to create a cookie based session

I am using postman client to make REST calls to JIRA API. It says "POST your credentials to http://jira.example.com:8090/jira/rest/auth/1/session" to get SESSION. I tried posting with Form-data, application/x-www-form-urlencoded, raw etc. Nothing worked. which is the right way to do that.
Here is the tutorial i am following: https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-cookie-based-authentication
Since you're using postman, I'm assuming you're in a dev environment. In this case, it might be simpler to get going with the auth header, which is a base-64 encoded username/password. From the documentation here:
Supplying Basic Auth headers
If you need to you may construct and send basic auth headers yourself. To do this you need to perform the following steps:
Build a string of the form username:password
Base64 encode the string
Supply an "Authorization" header with content "Basic " followed by the encoded string. For example, the string "fred:fred" encodes to "ZnJlZDpmcmVk" in base64, so you would make the request as follows.
curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" "http://kelpie9:8081/rest/api/2/issue/QA-31"
In the Headers section of Postman, add Authorization with Basic <base64-encoded-username:password>
Don't forget to also add the header Content-Type as application/json
(You can use base64encode.org to quickly encode your username/password).
Don't forget to put the string in as username-colon-password (username:password)
If you are on the same UI as I for postman, click Authorization, select an auth type (I used basic auth succesfully), and then enter your credentials. Next click over to the body tab, select raw, and on the drop down menu on the right choose JSON(applications/json), and supply the body as normal.
That is the first hurdle. The next hurdle which may be hit (and the one I am stuck on) is that once your basic-auth gets accepted, JIRA will deny access as part of Cross Site Request Forgery checks (XSRF) with a code 403. I have a ticket open right now seeing if there is a possible workaround to post and put from postman, because using postman and newman would be much much simpler than building an entire plugin which I have to jump through a bunch of hoops to access.
With Postman can simply add withCredentials:true to your request header section.