Payments unauthorized error


I just started to do payments things for my app. So I start with testing this all things. I opened app, put fake card information and in Log chat, app gave me back this.

curl --request POST
–header “Content-Type: application/json”
–header “Authorization: Bearer YOUR_ACCESS_TOKEN”
–header “Accept: application/json”
–data ‘{
“idempotency_key”: “d41294fe-eb25-4ebd-85a9-eb73a498b44a”,
“amount_money”: {
“amount”: 100,
“currency”: “USD”},
“source_id”: “cnon:CBASEFS57cH8ZgSVvPBpYVLNfHg”}’

So I replace YOUR_ACCESS_TOKEN with my access token and all this put in terminal. The terminal gave me back this error.

{“errors”: [{“code”: “UNAUTHORIZED”,“detail”: "Your request did not include an Authorization http header with an access token. The header value is expected to be of the format “Bear
er TOKEN” (without quotation marks), where TOKEN is to be replaced with your access token (e.g. “Bearer ABC123def456GHI789jkl0”).

What should I do ? Thanks for all advices

Hi @Johnny welcome to the forums!

Since you’re using the sandbox url, did you ensure you copied the sandbox access token and not the production access token? Typically UNAUTHORIZED means you’re using an invalid access token.

I use this token

Does it work if you use the API Explorer, or a tool like Postman? I think the terminal needs to be told that the command is continuing (by adding \ at the end of the line) so that could be an issue as well.

I tried everything and only in the Postman this work

When I click on this link Square API Explorer (API Explorer) I get this:
Snímka obrazovky 2021-05-27 180506

Then I also try that in Android Studio.
Fisrt I let space between end of url and \ like in the picture below

Then I tried delete space between end of url and \ like in the picture below and I get different error

So How can I resolve this problem in Android Studio? What is wrong ?

Honestly not 100% sure how to do curl/multiline commands in Android Studio. Is there a reason you’re trying to do it via terminal rather than via code (creating the HTTP request or using one of our SDKs)?

Based on your last screenshots, with the --header errors, it sounds like it’s still trying to execute them as separate commands, rather than one command but that’s specific to the Android Studio terminal, not to Square. In our In-App Payments SDK example we print out a curl command that should just be copy/paste to the terminal.

This works for me:

curl --request POST \                                                                                                                                           Ruby 2.6.6
--header "Content-Type: application/json" \
--header "Authorization: Bearer YOUR_ACCESS_TOKEN" \
--header "Accept: application/json" \
--data '{
"idempotency_key": "123",
"amount_money": {
"amount": 100,
"currency": "USD"},
"source_id": "456"}'

I tested it in Android Studio and it ran correctly.

I do that in terminal because I am new in this and I just followed steps in tutorial. So now I try do this in app and not in terminal and I will let you know. Thanks

Everything works fine

1 Like