Getting 401 'Unauthorized' error when using CURL, works OK when pasted in browser
When I paste the following URL in the address bar of browser: it works fine:
https://my-domain.zendesk.com/api/v2/users.json
However, when I attempt to use the following cURL call
curl https://my-domain.zendesk.com/api/v2/users.json -v -u my@email.com:my#password,
I get an error:
* Trying 104.16.51.111:443...
* Connected to my-domain.zendesk.com (104.16.51.111) port 443 (#0)
* schannel: disabled automatic use of client certificate
* ALPN: offers http/1.1
* ALPN: server accepted http/1.1
* Server auth using Basic with user 'my@email.com'
> GET /api/v2/users.json HTTP/1.1
> Host: my-domain.zendesk.com
> Authorization: Basic ***
> User-Agent: curl/7.83.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 401 Unauthorized
< Date: Wed, 02 Nov 2022 19:24:38 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 37
< Connection: keep-alive
* Authentication problem. Ignoring this.
< www-authenticate: Basic realm="Web Password"
< strict-transport-security: max-age=31536000;
< cache-control: no-cache
< x-zendesk-origin-server: classic-app-server-85767c77db-qrbvk
< set-cookie: _zendesk_cookie=***--***; path=/; expires=Thu, 02 Nov 2023 13:01:33 GMT; secure; HttpOnly; SameSite=None
< x-request-id: 763f4064c87917bd-EWR
< X-Request-ID: 763f4064c87917bd-EWR
< x-runtime: 0.058717
< X-Zendesk-Zorg: yes
< CF-Cache-Status: DYNAMIC
< Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=***"}],"group":"cf-nel","max_age":604800}
< NEL: {"success_fraction":0.01,"report_to":"cf-nel","max_age":604800}
< Set-Cookie: __cfruid=***; path=/; domain=.my-domain.zendesk.com; HttpOnly; Secure; SameSite=None
< Server: cloudflare
< CF-RAY: 763f4064c87917bd-EWR
<
{"error":"Couldn't authenticate you"}* Connection #0 to host my-domain.zendesk.com left intact
What could be the problem? My password contains special characters. I tried escaping them (ex. %23 for '#'), didn't work.
I have reviewed the documentation. I cannot find a "Troubleshooting" section specific to API.
I have an "Essentials"-level subscription. Does Zendesk have to activate something about my account so I can retrieve End Users?
-
Hi Pierre Clouthier,
This is indeed an encoding problem. Since your password contains special characters, you should use base64 encoding.
Use base64 command in bash or any similar tool:
echo 'myemail:mypassword' | base64
You should get something like `bXllbWFpbDpteXBhc3N3b3JkCg==`
Then instead of
-u myemail:mypassword,
Use
-H 'Authorization: Basic bXllbWFpbDpteXBhc3N3b3JkCg=='
Luckily, this problem has saved your exact password from being revealed in the post. However, I urge you to reset your password immediately.
-
Thank you very much for your reply.
I have tried this method, but I'm still getting rejected ("HTTP/1.1 401 Unauthorized").
Does Zendesk have to turn on an option for my account?
Why do I need to change my password? "my-domain" and " my@email.com:my#password" are made-up and fictitious.
BTW the example includes the trailing line feed (x0A) ("Cg==").
-
I have removed the special character from the password, and I am still getting "HTTP/1.1 401 Unauthorized", so that does not appear to be the problem.
Any suggestions?
-
Hi Pierre,
Have you enabled password API access for your account?
There is some related documentation here: https://support.zendesk.com/hc/en-us/articles/4408836402074-Using-the-API-dashboard#enabling_password_or_token_accessI hope that helps!
-
Hi James -
Thank you for your reply. API password access was indeed enabled for my account:
Still not working.
I'm using a password, not a Token.
Note that it works if I copy & paste the URL in my browser. It's just the cURL call that fails.
I would very much like it to work with cURL :o)
-
Hi Pierre,
Do you have 2-factor authentication enabled for your user profile? If you have 2-factor authentication enabled for your profile, you won't be able to use basic authentication. We call this out in the docs here.
-
Yes, I have 2FA activated. I will study the documentation and see if I can figure this out.
Thank you again.
-
HOORAY! It works!
The solution is to use:
curl https://my-domain.zendesk.com/api/v2/users.json -v -u my@email.com/token:my-token
The token is obtained in the Admin Center, "Apps and Integrations", "Zendesk API", Turn on Token Access, and "Add API token".
Suggestion: it would be really helpful if this were referenced in the Users API documentation, for example https://developer.zendesk.com/api-reference/ticketing/users/users/
Thank you again :o)
-
Congrats, Pierre! I'm glad to hear you got everything working.
Please sign in to leave a comment.
9 Comments