Bigcommerce php api returning null on initial app install

150 Views Asked by At

I'm trying to do initial install of a BigCommerce draft app for oAuth access to api using the official Bigcommerce php library.

For some reason no matter what I do I get null back from a post to BigCommerce api. Can anyone see anything that could be causing this?

Thanks for any advice

<?php

require_once ('vendor/autoload.php');

use Bigcommerce\Api\Connection as Connection;
use Bigcommerce\Api\Client as Bigcommerce;

error_reporting(E_ALL);
ini_set('display_errors', 'on');

$client_id = "*******";
$client_secret = "*******";

if (count($_GET)) {

    $code = $_GET['code'];
    $context = $_GET['context'];
    $scope = $_GET['scope'];

    $url = "https://login.bigcommerce.com/oauth2/token";

    $connection = new Connection();
    $connection->useUrlencoded();

    $connection->verifyPeer(false);

    $response = $connection->post($url, array(
    "client_id" => $client_id,
    "client_secret" => $client_secret,
    "redirect_uri" => "https://www.*******.com/bc-auth.php",
    "grant_type" => "authorization_code",
    "code" => $code,
    "scope" => $scope,
    "context" => $context));

    if (is_null($response)) {
        print "Last Error:\n";
        print "<pre>";
        print_r(Bigcommerce::getLastError());
        print "</pre>";
        exit();
    }

    $token = $response->access_token;

    die($token); // this is just for testing
}
1

There are 1 best solutions below

2
On

Try making your own POST request via cURL, papi..

// Prepare POST Data:
$postFields = array(
    'client_id'         =>      urlencode($client_id),
    'client_secret'     =>      urlencode($client_secret),
    'code'              =>      urlencode($code),
    'scope'             =>      urlencode($scope),
    'grant_type'        =>      urlencode('authorization_code'),
    'redirect_uri'      =>      urlencode("https://www.*******.com/bc-auth.php"),
    'context'           =>      urlencode($context)
);

// Format data for Post:
$fields_string = '';
foreach($postFields as $key=>$value) {
    $fields_string .= $key.'='.$value.'&'; 
}
rtrim($fields_string, '&');
$postFields = $fields_string; // Single URL string of post fields
unset($fields_string);       


// ** Curl Config **//
$ch = curl_init(); //open connection
curl_setopt($ch, CURLOPT_URL, "https://login.bigcommerce.com/oauth2/token");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch));     // execute post! & assign response
curl_close($ch);                 // close connection

Removing and inserting it instead of this code:

$connection = new Connection();
$connection->useUrlencoded();

$connection->verifyPeer(false);

$response = $connection->post($url, array(
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => "https://www.*******.com/bc-auth.php",
"grant_type" => "authorization_code",
"code" => $code,
"scope" => $scope,
"context" => $context));