Unable to access API V2 using access token

Hi Team,

I am unable to access upstox API V2 after generating code and access token.

I think you have to activate my profile for API. Please resolve this issue

Please find error
{“status”:“error”,“errors”:}
401

Please find Ticket number 7825427

Hi @nagarjuna ,

Thank you for sharing your Ticket no. with us.

Our concerned team is looking into the issue and trying to get it solved soon.

1 Like

@nagarjuna can you help us with your UCC along with request CURL and response you received from the API

1 Like

Hi Abishek,

I am using python , Please find below code

    qt_url = 'https://api-v2.upstox.com/user/get-funds-and-margin?'
    
    headers ={
        'accept': 'application/json',
        'Api-Version': '2.0' ,
        'Content-Type': f'Bearer {access_token}'
    }
    params = {"segment" : "SEC"}

    response = requests.get(qt_url,headers=headers,params=params)
    js_reponse = response.json()
    print(response.text)
    print(response.status_code)

Hi @AbhishekRathore ,

Please find ticket number 7825427 for more details

Thanks

Kindly utilize the provided code snippet. When passing the access_token , ensure to use “Authorization” as the key for this purpose.

qt_url = 'https://api-v2.upstox.com/user/get-funds-and-margin?'
    
headers = {
    'accept': 'application/json',
    'Api-Version': '2.0' ,
     "Content-Type": "application/x-www-form-urlencoded",
    'Authorization': f'Bearer {{access_token}}'
}
params = {"segment": "SEC"}

response = requests.get(qt_url,headers=headers,params=params)
js_reponse = response.json()
print(response.text)
print(response.status_code)

Hi @Pradeep_Jaiswar ,

I have modified the python code as per your above comment, Its not working , because the access_token is consider as a string not a variable.
‘Authorization’: f’Bearer {{access_token}}’

I am getting below error and status code. I am not sure whether you have executed or not
{“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}]}
401

FYI… The same access_token used for below end point , its working fine.

def get_historical(self,access_token,instrumentKey,symbol,interval,to_date, from_date):

    base_url = f'https://api-v2.upstox.com/historical-candle/{instrumentKey}%7C{symbol}/{interval}/{to_date}/{from_date}'
    
    headers ={
        'accept': 'application/json',
        'Api-Version': '2.0' ,
        'Content-Type': f'Bearer {access_token}'
    }
    response = requests.get(base_url,headers=headers)
    js_reponse = response.json()
    
    return js_reponse

EndDate =‘2023-08-01’ #to_date
StartDate= ‘2023-08-05’ #from_date

hist_data = get_historical(access_token,‘NSE_INDEX’,‘Nifty 50’,‘day’,StartDate,EndDate)
print(hist_data)

Result:-
{‘status’: ‘success’, ‘data’: {‘candles’: [[‘2023-08-04T00:00:00+05:30’, 19462.8, 19538.85, 19436.45, 19517, 0, 0], [‘2023-08-03T00:00:00+05:30’, 19463.75, 19537.65, 19296.45, 19381.65, 0, 0], [‘2023-08-02T00:00:00+05:30’, 19655.4, 19678.25, 19423.55, 19526.55, 0, 0], [‘2023-08-01T00:00:00+05:30’, 19784, 19795.6, 19704.6, 19733.55, 0, 0]]}}

As my observation there is know issue with code and access_token. I think its issue with end points
https://api-v2.upstox.com/user/get-funds-and-margin?
https://api-v2.upstox.com/user/profile

Rest assured that the provided code snippet has undergone thorough testing by our engineering team. For detailed information, kindly consult the API documentation available at this link

The historical candle data is efficiently cached on the CDN cache edge to enhance performance. This cached data can be accessed without requiring an access token to promote and early adoption.

1 Like

Hi @Pradeep_Jaiswar ,

Thanks for response, As per above screen shot we have to go with
‘Authorization’: f’Bearer {access_token}’ and not with
‘Authorization’: f’Bearer {{access_token}}'.

Yes, you are correct, I am able to pull data of historical-candle end point with out access_token.
Please let me know exact issue, once you receive info.

Thank You!!

Hi @Pradeep_Jaiswar ,

I have fix the issue by creating new API app with the new client id and client secret and change in local host in port.
Its working fine now. Please close the ticket.

Thank you all for your support.

1 Like