APIs & Integrations

Mrafey
Colaborador

Updating Contact Property Authorization Issue

Hello all!

I am getting a 401 unauthorized issue when trying to hit the following endpoint: 

https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/:email/

I have provided a authorization header within my fetch request in my code and I still get a unauthorized error when making the POST request.

Here is my code:

 

 

 

 

 

// HubSpot form submit event to store endangered animal result value into contact property
window.addEventListener('message', e => {
	if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit' && event.origin === document.location.origin) {
	
    let email = $("input[name=email]").val()
		
		updateContactProperty(animal, email);
		
	}
})

async function updateContactProperty(animal, email) {
const token = "my_app_token"
const data = {
	"properties": [
		{
			"property": "endangered_animal_quiz_result",
			"value": animal
		}
	]
}	

// 	make request to endpoint
	const res = await fetch(`https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/${email}/`, {
		mode: 'no-cors',
		method: 'post',
		headers: new Headers({
			'Authorization': `Bearer ${token}`,
			'content-type': "application/json"
		}),
		body: JSON.stringify(data)
	})

}

 

 

 

 

 


Would anyone have any idea what I may be doing wrong?

Thanks!

0 Me gusta
2 Respuestas 2
Jaycee_Lewis
Administrador de la comunidad
Administrador de la comunidad

Updating Contact Property Authorization Issue

Hey, @Mrafey 👋 Thanks for your post. I have a couple of questions:

  • have you tried sending this request without the mode: 'no-cors'? 
  • have you tried sending the same request via Postman? If so, does it work without giving an error? 

Thanks! — Jaycee


Join us on March 27th at 12 PM for the Digital Essentials Lab, an interactive session designed to redefine your digital strategy!
Engage with expert Jourdan Guyton to gain actionable insights, participate in live Q&A, and learn strategies to boost your business success.
Don't miss this opportunity to connect and grow—reserve your spot today!


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !
0 Me gusta
Mrafey
Colaborador

Updating Contact Property Authorization Issue

Hello @Jaycee_Lewis 👋

When not specifying the mode to 'no-cors' I end up getting a cors policy issue stating the following "has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource."


As for testing it on postman. The request works just as intended which is why I am confused why it doesn't work with the code I have in the Design Manager.

Here is my postman setup...

 

The following screenshot is showing the post request url, payload that I am sending which is updating the property "endangered_animal_quiz_result", followed by the result at the bottom which is a 200 response.

 

Screen Shot 2024-04-03 at 1.40.39 PM.png

 

The following is the authorization setup. I have removed my actual token value for obvious reasons, but here is the authentication which I have set to Bearer Token type.

 

Screen Shot 2024-04-03 at 1.42.29 PM.png

 

Works on postman, I have tested it a few times. But for some odd reason the code I have in my Design Manager for the module throws a 401 error....

0 Me gusta