BigCommerce OAuth “Invalid scope” error

2019-08-29 09:15发布

问题:

I'm attempting to retrieve an access token from BigCommerce. I'm following the instructions on this page: https://developer.bigcommerce.com/apps/callback

When I try to retrieve the access token, I am getting an invalid scope error. Here's the code:

    public function access_token_get(){

            print_r($_GET);

            $tokenUrl = "https://login.bigcommerce.com/oauth2/token";
            $connection = new Connection();
            $connection->setCipher('RC4-SHA');
            $connection->verifyPeer(false);
            $response = $connection->post($tokenUrl, array(
                "client_id" => "123456",
                "client_secret" => "123456",
                "redirect_uri" => "https://my-registered-auth-callback.com/",
                "grant_type" => "authorization_code",
                "code" => urlencode($_GET['code']),
                "scope" => urlencode($_GET['scope']),
                "context" => urlencode($_GET['context'])
            ));

            print_r($response);
            print_r($connection->getLastError());

            $token = $response->access_token;

            print_r($token);
    }

When this code runs, I get an empty $response. I added the getLastError() line to see what was going on, and it's outputting:

stdClass Object ( [error] => Invalid scope(s). )

These are the parameters output from the GET request:

Array ( [code] => 2idy1ozvee8s0ddlbg3jgquzgtr55gd [context] => stores/xxxxxx [scope] => store_v2_orders store_v2_products store_v2_customers store_v2_content store_v2_marketing store_v2_information_read_only users_basic_information )

Why would I be receiving this "invalid scopes" error? I also tried hardcoding a single scope to see if that works, for example, just doing "scope"=>"store_v2_orders", but when I do this, I get an error saying that the scope has not been granted by the user.

回答1:

Looks like the issue was that I did not need to urlencode the code, scope, and context. Removing the urlencode function fixed the issue.