我试图从BigCommerce检索访问令牌。我按照这个页面上的说明:https://developer.bigcommerce.com/apps/callback
当我尝试检索访问令牌时,我得到一个无效的作用域错误。下面是代码:
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);
}
当这段代码运行时,我得到一个空的$response
。我添加了getLastError()
行来看看发生了什么,它输出了:
stdClass Object ( [error] => Invalid scope(s). )
这些是GET请求输出的参数:
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 )
为什么我会收到这个"无效作用域"错误?我还尝试硬编码单个作用域,看看是否有效,例如,只是做"scope"=>"store_v2_orders"
,但是当我这样做时,我得到一个错误,说该作用域尚未被用户授予。
问题似乎是我不需要对代码、范围和上下文进行urlencode。删除urlencode函数修复了这个问题。