user3331682
user3331682

Reputation: 1

Generate Access Token in Bigcommerce

I have an issue when trying to generate Access Token by POST data to https://login.bigcommerce.com/oauth2/token. There is an exception error ('The remote server returned an error: (400) Bad Request.'). I don't know why but I already read the document at https://developer.bigcommerce.com/apps/callback#token

If I open that URL on any web browsers. It said that "The page you were looking for doesn't exist."

Could you please help me this?

Thank you, Trung

Upvotes: 0

Views: 1563

Answers (3)

J Z
J Z

Reputation: 315

If you are getting a 400 response to your POST request to https://login.bigcommerce.com/oauth2/token then that is indicating a problem with your data. The most likely causes are:

You are not including the following header in your POST request:

Content-Type: application/x-www-form-urlencoded

You are not URL encoding your POST data such as the following example:

client_id=236754&client_secret=m1ng83993rsq3yxg&code=qr6h3thvbvag2ffq&scope=store_v2_orders&grant_type=authorization_code&redirect_uri=https%3A%2F%2Fapp.example.com%2Foauth%26context%3Dstores%2Fg5cd38&context=stores%2Fabc123

Also note that the error response message body that you receive should have some more details about the source of the problem.

If you have confirmed the above points then maybe try giving a sample of your POST data or some information about what you are doing to URL encode your data. Make sure not to include your actual client ID, client secret, or redirect URI.

Upvotes: 1

Pavel Andreev
Pavel Andreev

Reputation: 71

Firstly you need to get temporary authorization code, but sending GET request to https://login.bigcommerce.com/oauth2/authorize with parameters clientId, Scope, Context ("stores/{your_store_hash}") and redirect_url.

Only after this, you can change your temporary token to permanent token (see previous post).

This permanent token expires in 30-60 days, but I don't know how to renew it automatically without user action. If you know that, please write how.

Upvotes: 0

Try ussing cURL

$data = array( "client_id" => "sdfgdfgdfkxddfgdfgdfdfgdfgddfgdfg2",
                    "client_secret" => "sdfgsdfgsdfgsdfgsdfgdf",
                    "redirect_uri" => "https://youapp.com/oauth",
                    "grant_type" => "authorization_code",
                    "code" => $_GET["code"], "scope" => $_REQUEST["scope"], "context" => $_GET["context"], );

    $postfields = http_build_query($data);

    $ch = curl_init();
    $url = "https://login.bigcommerce.com/oauth2/token";
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $output = curl_exec ($ch);
    curl_close ($ch);

$obj = json_decode($output);

var_dump($obj);

Upvotes: 0

Related Questions