【发布时间】:2018-08-07 09:32:29
【问题描述】:
我无法弄清楚我做错了什么。我正在为 BigCommerce 开发应用程序,但无法让简单的 oAuth 交换正常工作。
正在向https://www.my-app.com/oauth/bigcommerce/auth 发出初始获取请求。这是该请求的控制器中的代码。这是一个 Laravel 5.6 应用程序:
use Illuminate\Http\Request;
use Bigcommerce\Api\Client as Bigcommerce;
class BigcommerceOAuthController extends Controller
{
public function auth(Request $request)
{
$object = new \stdClass();
$object->client_id = 'my-client-id';
$object->client_secret = 'my-client-secret';
$object->redirect_uri = 'https://my-app.com/oauth/bigcommerce/auth';
$object->code = $request->get('code');
$object->context = $request->get('context');
$object->scope = $request->get('scope');
$authTokenResponse = Bigcommerce::getAuthToken($object);
$storeHash = str_replace('stores/', '', $request->get('context'));
Bigcommerce::configure(array(
'client_id' => 'my-client-id',
'auth_token' => $authTokenResponse->access_token,
'store_hash' => $storeHash
));
echo "<pre>";
print_r($authTokenResponse);
print_r(Bigcommerce::getTime());
echo "</pre>";
}
}
每次我尝试从 BigCommerce 控制面板安装草稿应用程序时,都会收到错误消息,因为 $authTokenResponse 不是对象。当我进一步调试Bigcommerce\Api\Connection类时,我可以看到来自服务器的响应是空的,状态是401,意思是“未授权”。
我无法弄清楚为什么会出现此错误。据我所知,我做的一切都是正确的。我尝试对从 $request->get('scope') 检索到的字符串进行 urlencoding,因为该字符串被 Laravel 未编码,但这似乎没有帮助。
我也很困惑这甚至应该如何工作。在 BigCommerce 文档中,他们展示了这个示例 POST 请求,它使用 application/x-www-form-urlencoded Content-Type 并将请求正文作为 url 编码字符串传递:
POST /oauth2/token HTTP/1.1 主机:login.bigcommerce.com 内容类型: application/x-www-form-urlencoded Content-Length: 186
client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}&code=qr6h3thvbvag2ffq&scope=store_v2_orders&grant_type=authorization_code&redirect_uri=https://app.example.com/oauth&context=stores/{STORE_HASH}
但是,如果您检查 Connection 类中发生的事情,您会看到 Content-Type 被设置为 application/x-www-form-urlencoded,正如文档所说,但请求正文正在传递作为 json 字符串,而不是 url 字符串。请求不应该是文档建议的 url 编码字符串吗?
【问题讨论】:
标签: oauth bigcommerce