0

I have the cURL command that works great:

curl https://{{mydomain}}.zendesk.com/api/v2/tickets.json \
  -d '{"ticket": {"subject": "Testing!", "type":"problem", "priority":"low", "custom_fields": [{"id": 25227383, "value": "support"}, {"id": 24746086, "value": "customer"}, {"id": 24621686, "value": "product"}, {"id": 24462503, "value": "yes"}], "comment": { "body": "Description" }}}' \
  -H "Content-Type: application/json" -v -u {{email}}/token:{{token}} https://obscura.zendesk.com/api/v2/users.json

And I tried to convert it to this in angular 5:

const body = {"ticket": 
{"subject": "Testing!",
"type":"problem", 
"priority":"low", 
"custom_fields": 
[{"id": 25227383, "value": "support"},
{"id": 24746086, "value": "customer"}, 
{"id": 24621686, "value": "product"}, 
{"id": 24462503, "value": "yes"}], 
"comment": { "body": "Description" }}};

this.http.post('https://{{mydomain}}.zendesk.com/api/v2/tickets.json', 
{data: body,
headers:{'Content-Type': 'application/json',
'Authorization': 'Bearer {{email}}/token:{{token}}',
'Access-Control-Allow-Origin': '*'}})
.toPromise().then(function(response) {
console.log(response.toString());
})

I'm getting the errors as below:

POST https://{{mydomain}}.zendesk.com/api/v2/tickets.json 401 (Unauthorized)

Failed to load https://{{mydomain}}.zendesk.com/api/v2/tickets.json: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 401.

ERROR Error: Uncaught (in promise): Response with status: 0 for URL: null
at resolvePromise (zone.js:824)
at resolvePromise (zone.js:795)
at eval (zone.js:873)
at ZoneDelegate.invokeTask (zone.js:425)
at Object.onInvokeTask (core.js:4620)
at ZoneDelegate.invokeTask (zone.js:424)
at Zone.runTask (zone.js:192)
at drainMicroTaskQueue (zone.js:602)
at ZoneTask.invokeTask [as invoke] (zone.js:503)
at invokeTask (zone.js:1540)

I assume that something is wrong with my authentication.

Does someone have any suggestions?

Thanks

2 Answers 2

2

You might want to make sure you're using OAuth authentication per ZenDesk's documentation: https://help.zendesk.com/hc/en-us/articles/115005580188-Making-cross-origin-browser-side-API-requests

They do not allow cross origin requests using basic authentication or an API token.

Sign up to request clarification or add additional context in comments.

1 Comment

Correct, request should be made via the server, to protect the token being exposed in the client side.
1

When you make an Http request directly using CURL, Postman, or something like that you don't need to set the CORS header. but now that you are making an Ajax call from your web app(Angular) you are making the request from another domain clocalhost) so you should set the header: Access-Control-Allow-Origin in your request. Also usually you need such configuration on the SERVER side as well which again usually is done through a config file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.