我试图从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"
,但是当我这样做时,我收到一条错误,指出用户未授予作用域。
答案 0 :(得分:0)
看起来问题是我不需要对代码,范围和上下文进行urlencode。删除urlencode函数修复了问题。