Error in getting Token

I did…
import requests

Upstox API details

payload = {
‘client_id’: ‘YOUR_CLIENT_ID’, # Replace with your client ID
‘redirect_uri’: ‘YOUR_REDIRECT_URI’, # Replace with your redirect URI
‘response_type’: ‘code’
}

headers = {
‘accept’: ‘application/json’,
‘Api-Version’: ‘2.0’,
‘Content-Type’: ‘application/x-www-form-urlencoded’
}

url = “https://login.upstox.c … .etc / v2/ oauth/ authorize”

response = requests.get(url, headers=headers, params=payload)

Print the URL to visit for authorization

print(response.url)

Upstox API endpoint for token exchange

token_url = "https: //api-v2.upstox.c…,etc etc /authorization/token "

Replace with the actual authorization code you received

authorization_code = ‘YOUR_AUTHORIZATION_CODE’

Payload for the token exchange request

payload_token = {
‘code’: authorization_code,
‘client_id’: ‘YOUR_CLIENT_ID’, # Replace with your client ID
‘client_secret’: ‘YOUR_CLIENT_SECRET’, # Replace with your client secret
‘redirect_uri’: ‘YOUR_REDIRECT_URI’, # Replace with your redirect URI
‘grant_type’: ‘authorization_code’
}

Headers for the request

headers_token = {
‘Content-Type’: ‘application/x-www-form-urlencoded’,
‘Accept’: ‘application/json’
}

Send the POST request to exchange the authorization code for an access token

response_token = requests.post(token_url, headers=headers_token, data=payload_token)

Handle the response

if response_token.status_code == 200:
access_token_data = response_token.json()
print(“Access Token:”, access_token_data[‘access_token’])
else:
print(“Failed to get access token:”, response_token.text)
And as Output I got the “URL”(which is working fine) and "Failed to get access token: “This API is deprecated, please migrate to Upstox API v2. For more details refer upstox documentation”

@Overkill,

Thank you for contacting us.

Based on the error message you provided, it appears that you are using a URL that is no longer valid.

Could you please share the cURL object for the token API call you are making? This information will help us pinpoint and address the issue more efficiently.

Alternatively, you might want to try the following cURL command to generate an access_token, making sure to replace the placeholders with appropriate values:

curl -X 'POST' 'https://api.upstox.com/v2/login/authorization/token' \
-H 'accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'code={your_code}&client_id={your_client_id}&client_secret={your_client_secret}&redirect_uri={your_redirect_url}&grant_type=authorization_code'

You can find a sample code for the token operation here.

Thanks!

Hey, thanks for the reply. I managed to solve it. I got the access token. But just only for a single time. when I run the code the next time I’m having the UDAPI100057 error

@Overkill,

That’s great to hear!

Regarding the UDAPI100057 error, the documentation specifies that the code is valid for single use only.

I suggest going through the documentation carefully, as it includes important details that will be helpful when you are setting things up.

Thanks!