MailChimp 3.0 HTTP POST Json Example - java

I'm trying to make use of MailChimp's Automation trigger that activates when a subscriber's email ID is posted to an API endpoint:
(link)
Following the Workflow instructions, I'm using the following https://us19.api.mailchimp.com/3.0/automations/xxxxxxxxxx/emails/xxxxxxxxxx/queue URL provided via the Campaign.
I can successfully send an authenticated HTTP POST request (using this do_Post() method copied into my codebase), but am faced with a 400 error with a Content-Type: application/problem+json response. My JSON is incorrect and I'm not sure what the JSON should look like to trigger the above event.
I've tried a plethora of different cracks at the JSON - my latest attempt looks like {"unique_email_id":"e063dfcf4g"}. Every attempt at altering the JSON data still is returning me the same error above.
I've been wondering if I possibly need to set some more headers in the POST request, but this is beyond my area of expertise.
I appreciate the help and insights.

Here's an example of HTTP Basic Auth Postman request:
POST URL:
https://us19.api.mailchimp.com/3.0/automations/********/emails/********/queue
you'll get this when you create api based trigger automation campaign.
Authorization:
generate the api key from mailchimp dashboard and use that as password in postman authorization. put any username.
POST Body:
{"email_address":"tigerking#gmail.com"}
Read more about mailchimp API : https://mailchimp.com/developer/guides/get-started-with-mailchimp-api-3/
there's also a npm module for this https://npmjs.com/package/mailchimp-api-v3

Related

RestTemplate GET call gives 422

I am in the process of integrating with a third party app and this issue comes while integrating with one of their GET APIs. The API has some headers and params and it's working perfectly fine in Postman. However, the same request when formed in Java and executed with RestTemplate (exchange method) gives 422 unparsable entity. Where could I be going wrong and what could be done to fix this issue?
It really depends on the server end why it responds with that HTTP code.
Definition of 422 Unprocessable Entitycode from Mozilla website:
The HyperText Transfer Protocol (HTTP) 422 Unprocessable Entity response status code indicates that the server understands the content type of the request entity, and the syntax of the request entity is correct, but it was unable to process the contained instructions.
which means, the data is correct, and server understood it but it cannot process the request.
I suggested you try in RestTemplate using the same exact values tested successfully on Postman.
A screenshot on Postman and your some snapshot of your code on the headers and request params will help to debug further.
Make sure you don't mistake #PathVariable with #RequestParam.

How to fix code 215: Bad Authentication data on Twitter Oauth (Direct Messages)?

UPDATE 2
SOLVED, I was missing Header String tutorial instructions at https://developer.twitter.com/en/docs/basics/authentication/guides/authorizing-a-request.html
Answer to my question: Headers must not contain any json data.
Here's my fully functional project, just insert your credentials and you are ready to send Twitter direct messages:
https://github.com/ricarrrdo/gs-rest-service
Note: I'm using Spring deprecated AsyncRestTemplate, so if someone knows how to use the new WebClient, please upgrade restASyncClientBody() method inside Application.java - it would be much appreciated!
UPDATE 1
https://github.com/ricarrrdo/gs-rest-service/blob/master/src/main/java/hello/Application.java#L2-L12
This link above is my spring boot project which recreates Twitter given example at https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature.html to authenticate and authorize a API request.
It's working fine, just compile/run with mvn spring-boot:run and watch console output.
You can check lines 41-52, which are the same parameters used in Twitter example.
Problem/Question
I'm trying to send a Direct Message on Twitter via Twitter API but I keep getting Bad Authentication data error. How should I change my code to be able to send direct messages?
I mean: In https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature.html there's an example with how to create a valid Oauth header using x-www-form-urlencoded parameters. But how can I create valid Oauth headers when sending Json?
Twitter tutorial for direct messages here -
https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event
Notes
Authorization header needed to send a direct message:
authorization: OAuth oauth_consumer_key="YOUR_CONSUMER_KEY",
oauth_nonce="AUTO_GENERATED_NONCE",
oauth_signature="AUTO_GENERATED_SIGNATURE",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="AUTO_GENERATED_TIMESTAMP",
oauth_token="USERS_ACCESS_TOKEN", oauth_version="1.0"

Read POST form in Angular submitted by external websites

I am developing a website with Java for the backend and Angular for frontend. There is a situation when some external websites may send data to my website using POST form. For instance,
▼ General
Request URL: https://myangularwebsite/
Request Method: POST
...
▼ Request Headers
Content-Type: application/x-www-form-urlencoded
Host: myangularwebsite
Origin: https://externalwebsite
Referer: https://externalwebsite/send.form?id=0
...
▼ Form data
ID: 0000000
TIME: 2017.06.04 11:53:58
SIGNATURE: ...geirgmGKFGJWR...
...
Now, I need to capture the form in Angular somehow, send/redirect it to the backend to validate the signature and return the answer back to Angular to proceed working with this website.
I tried posting to my website to test how it might work using Postman, but get Cannot POST /.
I know how to work with GET and URL query parameters in Angular but I think I need to process a POST request based on headers I see with Chrome DevTools 'Network' section when coming from externalwebsite to myangularwebsite.
Should I dedicate a route in the backend and expose it, for example, .../api/external in my backend and tell these websites to use this link instead of directly posting to my Angular website's homepage?
I have already read another question ( How to read form post data in Angular 2 typescript? ) which is somewhat similar but I do not think using PHP is the right way for me as the website I am developing already has an older version written in PHP.
The answer at the link you provided is correct: you cannot do it in just Javascript, you have to use some server-side code. They mention PHP as an example, but any server-side component will do, and as you have Java at your backend, let it be Java.
So, when an HTTP request comes from an external site, you have to use a server-side component to handle it. But there are some options.
If this request is made using your user browser (so it is something like a redirect, but using a POST method), then you can do the following: catch that request at your backend, output some javascript with some data to the user's browser and process that data in your Angular code. Or this could be a redirection to your main Angular entry point, it is up to you.
If this request is made by some other means (for example, this is a server-to-server request made with with curl like a notification from a Credit Card processing), with no browser involved, then you don't need to have any Javascript (Angular or whatever it could be) as they are needed for browser only. In this case you just handle the request at your server-side.
In both cases, it seems plausible to dedicate some special endpoint for handling (or landing) such externally-originated requests.

Request Paypal's IPN to send Authorization token in headers to my webhook

I've been struggling with Paypal's IPN for a couple of days now because I've done the IPN webhook thing before without problem, but this time messages weren't received by my Web service.
After some thinking I found that I was getting a 401 status code from paypal because my service API is under some security with Swagger which requires me to pass an HTTP header with this format:
Authorization: Bearer foobar
However I haven't found in Paypal's docs if this is possible, I mean to add an authorization token along with the transaction send my Paypal, so my API is able to accept the message.
This question suggests to make use of the IPN's custom variable, which might be OK if I wanted to send the DB user's ID or something like that, however I don't think it might be useful in this case due to the message being rejected by Swagger before being processed at all...
Not sure if useful for the question but:
Our webservice is made with Java
I have configured the URL in Paypal's sandbox and got a 401 status code
The webservice is as simple as printing the transaction's ID sent by Paypal in console.
The way I call Paypal's IPN is with Paypal generated buttons

HTTP 403 Forbidden error for some contextio 2.0 requests signed using scribe oauth service

I wrote a context.io 2.0 java client for : accounts,discovery, threads and messages operations to be used in my project (based on the available ContextIO Java client).
My code is at : https://github.com/dileepajayakody/isis-reputationbox/blob/master/reputationbox/dom/src/main/java/at/tomtasche/contextio/ContextIO_V20.java
While testing it, when I invoke the accounts request: https://api.context.io/2.0/accounts and discovery requests (eg: https://api.context.io/2.0/discovery?email=testemail#gmail.com&source_type=IMAP, I successfully get the desired response with a HTTP 200
However when I invoke the messages, threads, contacts requests by adding the account_id parameter in the request URL (eg : https://api.context.io/2.0/accounts/1234ff425ad/messages) I get an empty JSON array with a HTTP 403 Forbidden response.
When I try out the same request in the contextio developer console, I get the response without a problem.
I don't think it's something wrong with the way I sign the oauth request since it's the same way I sign for accounts and discovery requests for which I get the contextIO response properly.
Any help in resolving this error is much appreciated.
Thanks,
Dileepa
I don't think the issue is related to oauth request signing, since we normally return 401 or 404 messages for errors there.
The fact there's no body content with the 403 response leads me to believe your api_key does not own the account specified. If it did own it then we would include a json array with type, code, value parameters to help debug the issue. If you have two developer accounts then you may just be using the wrong one for this test.
I hope this helps. If not, please feel free to contact us at support#context.io.
Thanks!
Dan

Categories