Need Help with "Invalid Token" Error (errorCode: UDAPI100050)

Hello community,

I’m currently integrating Upstox API V2 into my application, and I’m facing an authentication issue that’s causing an “Invalid token used to access API” error (errorCode: UDAPI100050). I think you have to activate my profile for API. Please resolve this issue.I’ve tried troubleshooting the problem on my own, but I could use some guidance from the community.

Error:
{“status”:“error”,“errors”:[{“errorCode”:“UDAPI100050”,“message”:“Invalid token used to access API”,“propertyPath”:null,“invalidValue”:null,“error_code”:“UDAPI100050”,“property_path”:null,“invalid_value”:null}]}

Thank you for reaching out.

Could you kindly assist me by providing the complete CURL command or sample code that you are executing?

from future import print_function
import time
import upstox_client
from upstox_client.rest import ApiException
from pprint import pprint

Configure OAuth2 access token for authorization: OAUTH2

configuration = upstox_client.Configuration()
configuration.access_token = ‘A3bR9k’

create an instance of the API class

api_instance = upstox_client.UserApi(upstox_client.ApiClient(configuration))
api_version = ‘V2’ # str | API Version Header

try:
# Get profile
api_response = api_instance.get_profile(api_version)
pprint(api_response)
except ApiException as e:
print(“Exception when calling UserApi->get_profile: %s\n” % e)

I am currently using the same code, and all the API installations are completed. During the API creation process, I did not subscribe to any Interactive or Historical API services. Consequently, I would like to know if it is necessary for me to subscribe to these services.

Please take note that including the API version as 2.0 is required.

I presume that the access token provided in this example is fictitious. Furthermore, could you please elaborate on the method you employ to retrieve the access token after the login process? For further information, you can refer to: Get Token | Upstox Developer API.

Hi Pradeep,

I am getting same error given below.

TTP Error 401: {“status”:“error”,“errors”:[{“errorCode”:“UDAPI100050”,“message”:“Invalid token used to access API”,“propertyPath”:null,“invalidValue”:null,“error_code”:“UDAPI100050”,“property_path”:null,“invalid_value”:null}]}

The Code i am using is as follows.

import requests
import CONFIG as con

api_method = “GET”
api_endpoint = “user/profile”
access_token = {con.access_token}
content_type_header = “application/json”

url = f"https://api-v2.upstox.com/{api_endpoint}"

headers = {
“accept”: “application/json”,
“Api-Version”: “2.0”,
“Authorization”: f"Bearer {access_token}",
}

Add the content type header if provided

if content_type_header:
headers[“Content-Type”] = content_type_header

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

if response.status_code == 200:
# Successful response
data = response.json()
print(data)
else:
# Error handling for non-200 status codes
print(f"HTTP Error {response.status_code}: {response.text}")

Below is the operational code for the ‘get profile’ API. It has been tested using a valid access token. Please verify if this code works for you. Additionally, kindly provide your UCC for us to track your request.

import requests
import json

profileUrl  = f"https://api-v2.upstox.com/user/profile"
accessToken = "{your_access_token_here}"

headers = {
    "accept": "application/json",
    "Api-Version": "2.0",
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization" : f"Bearer {accessToken}"
}
response = requests.get(profileUrl, headers=headers)

Please Note :

After logging in, you will need to generate an access token by utilizing the Get Token API. Detailed information can be found in the documentation available at this link: Upstox Developer API - Documentation of investment and trading platform developer API

In one of your previous discussions, I noticed that you attempted to use the auth code as the access token, which would lead to an invalid access token.

configuration.access_token = ‘A3bR9k’

I’m sorry but this doesn’t work, Please find the screenshot below

I got the access token after constructing the url, i have replaced it with ‘xxxx’ here for security reasons

Please provide your UCC and the URL for generating the access token.

My UCC is DW4748 and my url is https://api-v2.upstox.com/login/authorization/dialog?response_type=code&client_id=xxxx&redirect_uri=https%3A%2F%2F127.0.0.1%3A5000%2

I have removed my client id from the URL, I don’t know if it is safe to post in public forum

From the above URL (https://api-v2.upstox.com/login/authorization/dialog?response_type=code&client_id=xxxx&redirect_uri=https%3A%2F%2F127.0.0.1%3A5000%2) you have mentioned you will get a one time to auth code which you need to use for generating the access token.

You can obtain the access token from the https://api-v2.upstox.com/login/authorization/token endpoint and use it in subsequent requests to make API calls. Below is the sample working code to get an access token, It has been tested using a valid auth code.

import requests
import json

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

auth_url = "https://api-v2.upstox.com/login/authorization/token"
data = {
    "code": "{your_auth_code}",
    "client_id": "{your_client_id}",
    "client_secret": "{your_client_secret}",
    "redirect_uri": "https://localhost",
    "grant_type": "authorization_code",
}

response = requests.post(auth_url, headers=headers, data=data)


Still Doesn’t work

All your segments are currently in a dormant state.

To activate the necessary segment, please access the segment section within your account or by clicking on the following link after login: Upstox Accounts

Screenshot 2023-09-20 at 12.36.20 PM