mailjet-apiv3-java icon indicating copy to clipboard operation
mailjet-apiv3-java copied to clipboard

delete customer 401 Unauthorized

Open lrkwz opened this issue 1 year ago • 1 comments

When I perform a delete customer I always receive a 401 code.

The client is initialized the "standard" way:

final ClientOptions options = ClientOptions.builder()
                .apiKey(apiKey)
                .apiSecretKey(apiSecretKey)
                .build();
        final MailjetClient client = new MailjetClient(options);

Example given a function that returns the customer ID (assuring he/she exists)

    @Test
    public void removeContact() throws MailjetException {
        final MailjetRequest request = new MailjetRequest(Contact.resource, getContactIdByEmail("[email protected]"));
        final MailjetResponse response = client.delete(request);
        assertThat(response.getStatus()).isEqualTo(200);
    }

output is:

com.mailjet.client.errors.MailjetUnauthorizedException: Unauthorized. Please,verify your access key and access secret key or token for the given account

	at com.mailjet.client.MailjetResponseUtil.validateMailjetResponse(MailjetResponseUtil.java:32)
	at com.mailjet.client.MailjetClient.parseResponse(MailjetClient.java:297)
	at com.mailjet.client.MailjetClient.delete(MailjetClient.java:240)
	at learning.MailJetApiTest.removeContact(MailJetApiTest.java:145)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

Same if I use a filter such as:

    @Test
    public void removeContactByMail() throws MailjetException {
        final MailjetRequest request = new MailjetRequest(Contact.resource)
                .filter(Contact.EMAIL, "[email protected]");
        final MailjetResponse response = client.delete(request);
        assertThat(response.getStatus()).isEqualTo(200);
    }

lrkwz avatar Feb 29 '24 18:02 lrkwz

As a workaround I used the GDPR delete feature as described in the user guide :

   @Test
    public void removeContactByID() throws IOException {
        final String randomName = generateRandomUsername();
        final int contactID = addContact(randomName, randomName + "@example.com");

        final String base64Credentials = Base64.getEncoder().encodeToString((API_KEY + ":" + API_SECRET_KEY).getBytes());

        final Request request = new Request.Builder()
                .url("https://api.mailjet.com/v4/contacts/" + contactID)
                .addHeader("authorization", "Basic " + base64Credentials)
                .addHeader("user-agent", "curl/7.54.1") // !!!!
                .delete()
                .build();
        final OkHttpClient httpClient = new OkHttpClient();
        try (final Response response = httpClient.newCall(request).execute()) {
            assertThat(response.isSuccessful()).isTrue();
        }
    }

Contact details are immediately anonymized and all records will be deleted after 30 days. (...) The anonymized contact will retain its contact ID and general configuration settings until it is removed when the 30-day period ends.

So it is useless to test if the deletion has really run after the call.

lrkwz avatar Mar 02 '24 14:03 lrkwz