Share Your Work

Mrafey
投稿者

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 いいね!
2件の返信
Jaycee_Lewis
コミュニティーマネージャー
コミュニティーマネージャー

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

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 いいね!
Mrafey
投稿者

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 いいね!