Got Error while Fetching Access Token Via CURL using PHP



  • Hello,
    I am using curl in my local server with PHP. Its shows errors "Missing required request parameters: [Authorization]"
    event i used username and password.
    Here is my code snipet:

    <?php
    include "config.php";

    $code = $_GET['code'];

    $url = 'https://api.upstox.com/index/oauth/token';

    $header = array();
    $header[] = 'Content-Type: application/json';
    $header[] = 'x-api-key: ' . UPSTOX_API_KEY;

    $data = array(
    'code' => $code,
    'grant_type' => 'authorization_code',
    'redirect_uri' => UPSTOX_RETURN_URL,
    );

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data) );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_USERPWD, UPSTOX_API_KEY . ":" . UPSTOX_API_SECRET );
    $returndata = curl_exec($ch);
    $errordata = curl_error($ch);
    curl_close($ch);


  • Developer

    Hi @jlrangani ,

    Try commenting these line

    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_USERPWD, UPSTOX_API_KEY . ":" . UPSTOX_API_SECRET );

    and add these
    in place of it

    $encodedAuth = base64_encode(UPSTOX_API_KEY . ":" . UPSTOX_API_SECRET);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization : Basic ".$encodedAuth));
    

    Thanks,
    Alok Gaira



  • Thanks for your quick response, its working now.

    Thanks agian.


  • Developer

    @jlrangani
    Finally, someone thanked me, You made my day.



  • Hello,
    I have another error can you please resolve this one, when i requsest OHLC data for NIFTY JUL FUT data its show error

    'XXXXXXaccess_tokenxxxxxxxx' not a valid key=value pair (missing equal-sign) in Authorization header: 'Bearer XXXXXXaccess_tokenxxxxxxxx'

    Here is my code snipet:

    $url = 'https://api.upstox.com/historical/ohlc/nse_fo/NIFTY17JULFUT?interval=60MINUTE';

    $data		= array();
    
    $header		= array();
    $header[] 	= 'Authorization: Bearer ' . UPSTOX_ACCESS_TOKEN;
    $header[] 	= 'Content-Type: application/json';
    $header[] 	= 'x-api-key: ' . UPSTOX_API_KEY;	
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);	
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data) );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
    
    $returndata = curl_exec($ch);
    $errordata 	= curl_error($ch);
    curl_close($ch);


  • @AlokGaira
    Haha. Don't worry my friend, there will be good days, and bad


  • Developer

    @jlrangani

    This request is a GET request, not a POST request

    Comment these lines

    //curl_setopt($ch, CURLOPT_POST, 1);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data) );
    
    

    Please check the docs carefully

    https://upstox.com/developer/api/v1/docs/#ohlc

    Thanks,
    Alok Gaira



  • Thank you, its working now.

    Upstox has really good support staff, which are providing quick response and accurate solution.

    Thanks again.



  • I followed the same code as he mentioned in the first comment and made change as per AlokGaira.
    But I am getting the following response from the API.

    {"message":"Forbidden"}

    What could be the error?


  • Developer

    @ranismariya - You are missing with some required parameters.

    Please check if you are passing x-api-key and authorization header in request.

    Thanks,


Log in to reply
 

Looks like your connection to Upstox Forum was lost, please wait while we try to reconnect.