USPS / api-examples

62 stars 12 forks source link

Getting an `invalid_client` error when trying to get OAuth token #27

Open scriptprojectsdev opened 3 months ago

scriptprojectsdev commented 3 months ago

Describe the bug I created my account today and I'm trying to get an OAuth token and I'm getting an invalid_client error. I'm doing a POST request to https://api.usps.com/oauth2/v1/token the body of the token has the 5 required parameters grant_type: "client_credentials" client_id: "xxxxxxxx" client_secret: "xxxxxx" customer_registration_id: "xxxxxx" mailer_id: "xxxxxxx"

To Reproduce Steps to reproduce the behavior:

  1. Do a post request to https://api.usps.com/oauth2/v1/token with a newly created account
  2. Add the 5 required parameters from above
  3. invalid_client error shows up

Expected behavior I expected to receive the OAuth token

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context No additional context

ghost commented 3 months ago

I am sorry that you are having problems with your first API call.

A few questions:

If you are using PostMan, then make sure that you have the Accept header set to either application/json, or application/x-www-form-urlencoded, then set the other PostMan options accordingly.

For the V1 OAuth API, you need a valid customer registration system identifier AND a matching Mailer ID to authenticate. This is in addition to the consumer key and secret you received by using the Developer Portal.

If you selected an API product when you received your keys, then you should have the rights to consume this API. You will not if you did not select any API product from the list during application registration.

Lastly, your client identifier may have been revoked.

I would recommend using V3 OAuth APIs. You will have to onboard you app be signing in with your customer registration system credentials then granting access to the app you wish to set up.

grakes87 commented 2 months ago

I'm trying to do this as well and I have all of the information you told the op set properly and am still getting this error.

nandorholozsnyak commented 2 months ago

V1 is not really working for me too, but the V3 did.

One note here, if you are copying the instructions from the README file, make sure you replace any of the environment variables as they will not be resolved in the JSON but the plain environment variable names will be sent over.

So the example from the readme.

curl -X 'POST' "https://api.usps.com/oauth2/v3/token" \
     --header 'Content-Type: application/json' \
     --header 'Accept: application/json' \
     --data '{
        "client_id": "'$CLIENT_ID'",
        "client_secret": "'$CLIENT_SECRET'",
        "grant_type": "client_credentials"
        }'

Make sure your $CLIENT_ID and $CLIENT_SECRET is replaced with the real value. On the other hand your APP should be "approved" via this site:

This somehow solved me these issues, but it is more than frustrating that the docs are not really showing that for example the V1 Auth API is "deprecated" or not really working. Good luck!!

tyre commented 1 month ago

What nandorholozsnyak said worked for me. The instructions look like your shell will interpolate the env variables but they won't