skip to Main Content

I have the following code to get Access token and it returns access token, but tje response contains not refresh token. What could be the reason?

$token_url = 'https://login.bigcommerce.com/oauth2/token';
$post_data = [
    'client_id' => $client_id,
    'client_secret' => $client_secret,
    'code' => $code,
    'grant_type' => 'authorization_code',
    'redirect_uri' => $redirect_uri,
];

2

Answers


  1. Chosen as BEST ANSWER

    Searching further, I got the information that BigCommerce access tokens are generally long-lived, meaning they do not expire frequently. You might not need a refresh token if your access token is valid for an extended period.


  2. This following code helps you to fetch the access token from the BigCommerce OAuth2 response and checks for the presence of a refresh token. Make sure that your sensitive data should be loaded from the Environment variables

    $token_url = 'https://login.bigcommerce.com/oauth2/token';
    
    $post_data = [
        'client_id' => getenv('BIGCOMMERCE_CLIENT_ID'), // Load from environment variable
        'client_secret' => getenv('BIGCOMMERCE_CLIENT_SECRET'), // Load from environment variable
        'code' => $code,
        'grant_type' => 'authorization_code',
        'redirect_uri' => $redirect_uri,
    ];
    
    // Initialize cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $token_url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    
    // Check for cURL errors
    if ($error) {
        echo 'Error retrieving token: ' . $error . PHP_EOL;
        exit(1);
    }
    
    // Decode the JSON response
    $response_data = json_decode($response, true);
    
    // Check for JSON decoding errors
    if (json_last_error() !== JSON_ERROR_NONE) {
        echo 'Error decoding JSON response: ' . json_last_error_msg() . PHP_EOL;
        exit(1);
    }
    
    // Handle the response
    if (isset($response_data['access_token'])) {
        $access_token = $response_data['access_token'];
        echo 'Access Token: ' . $access_token . PHP_EOL;
    
        if (isset($response_data['refresh_token'])) {
            $refresh_token = $response_data['refresh_token'];
            echo 'Refresh Token: ' . $refresh_token . PHP_EOL;
        } else {
            echo 'Refresh Token not provided in the response.' . PHP_EOL;
        }
    } else {
        // Handle error from BigCommerce (check for error codes in response)
        echo 'Error retrieving access token: ' . (isset($response_data['error']) ? $response_data['error'] : $response) . PHP_EOL;
    }
    
    

    BigCommerce OAuth2 implementation might not always include a refresh token in the initial response. It depends on your application configuration and permission requested.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search