Re: Random Unauthorized Error

hassangamal
New Contributor

Random Unauthorized Error

I randomly received an "Unauthorized" response, but after multiple attempts, I got a 200 response, and it worked fine. I'm not sure what the issue is.

5 REPLIES 5
hassangamal
New Contributor

Re: Random Unauthorized Error

this is my request body 

curl --location 'https://sandbox.api.visa.com/cofds-web/v1/datainfo' \
--header 'keyId: {keyId} \
--header 'ex-correlation-id: WH1D2K3A9VFG_SC' \
--header 'Authorization: Basic Convert.ToBase64String Encoding.UTF8.GetBytes(userId + ":" + password));

' \
--header 'Content-Type: application/json' \
--data '{
"encData": "eyJhbGciOiJSU0EtT0FFUC0yNTYiLCJlbmMiOiJBMTI4R0"
}'

hassangamal
New Contributor

Re: Random Unauthorized Error

{
"responseStatus": {
"status": 401,
"code": "9210",
"severity": "ERROR",
"message": "Token validation failed",
"info": ""
}
}
SyedSa
Community Moderator

Re: Random Unauthorized Error

Hi @hassangamal, Thank you for reaching out! An agent will get back to you as soon. Until then, if any community member knows a solution, feel free to reply in this thread.

hassangamal
New Contributor

Re: Random Unauthorized Error

this is my response 

Im getting the same response using Insomnia client.

{
"responseStatus": {
"status": 401,
"code": "9122",
"severity": "ERROR",
"message": "Authentication failed",
"info": ""
}
}

DianaVisaPM
Visa Developer Support Specialist

Re: Random Unauthorized Error

Hey @hassangamal,

 

The "Unauthorized" response (HTTP status code 401) indicates that there was an issue with the authentication of your request. This can happen for several reasons, such as incorrect credentials, expired tokens, or issues with the authorization header. Here are some steps to help you troubleshoot and resolve this issue:

 

Steps to Troubleshoot Unauthorized Response

1. Check Credentials: Ensure that the `userId` and `password` used in the `Authorization` header are correct and have not expired.

2. Base64 Encoding: Make sure that the `Authorization` header is correctly encoded in Base64 format. In your cURL command, it should be:
```sh
--header 'Authorization: Basic <Base64EncodedUserId:Password>'
```

You can generate the Base64 encoded string using the following command in a Unix-based system:
```sh
echo -n 'userId:password' | base64
```

3. Correct Header Syntax: Ensure that the headers are correctly formatted and do not have any missing or extra characters. Your cURL command should look like this:

```sh
curl --location 'https://sandbox.api.visa.com/cofds-web/v1/datainfo' \
--header 'keyId: {keyId}' \
--header 'ex-correlation-id: WH1D2K3A9VFG_SC' \
--header 'Authorization: Basic <Base64EncodedUserId:Password>' \
--header 'Content-Type: application/json' \
--data '{
"encData": "eyJhbGciOiJSU0EtT0FFUC0yNTYiLCJlbmMiOiJBMTI4R0"
}'
```

 

4. Consistent Environment: Since you mentioned that the issue is intermittent, ensure that there are no network issues or server-side problems that could be causing the inconsistency. Sometimes, transient issues on the server or network can lead to intermittent authentication failures.

5. Token Expiry: If you are using tokens, make sure they are not expired. Some tokens have a short lifespan and may need to be refreshed frequently.

6. Time Synchronization: Ensure that the system time on your server is synchronized with a reliable time source. Authentication tokens often rely on accurate timestamps.

 

Example cURL Command

Here is an example of a properly formatted cURL command:

```sh
curl --location 'https://sandbox.api.visa.com/cofds-web/v1/datainfo' \
--header 'keyId: YourKeyId' \
--header 'ex-correlation-id: WH1D2K3A9VFG_SC' \
--header 'Authorization: Basic <Base64EncodedUserId:Password>' \
--header 'Content-Type: application/json' \
--data '{
"encData": "eyJhbGciOiJSU0EtT0FFUC0yNTYiLCJlbmMiOiJBMTI4R0"
}'
```

Replace `<Base64EncodedUserId:Password>` with the actual Base64 encoded string of your `userId:password`.

 

Additional Considerations

- API Rate Limiting: Check if there are any rate limits imposed by the API that could be causing the `Unauthorized` response after multiple attempts.
- Insomnia and Other Clients: If you are getting the same response using Insomnia or other API clients, ensure that the request configuration is consistent across all tools.

 

By following these steps, you should be able to identify and resolve the issue causing the "Unauthorized" response.




Thanks,

Diana



Was your question answered? Don't forget to click on "Accept as Solution" to help other devs find the answer to the same question.