【问题标题】:BigCommerce oAuth auth token request always returning 401BigCommerce oAuth 身份验证令牌请求始终返回 401
【发布时间】: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-&gt;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


    【解决方案1】:

    这里有几件事要检查:

    当您拥有 client_id 和 secret_id 时。您应该拥有向https://login.bigcommerce.com/oauth2/token 的 BC Auth Token Service 发送 POST 请求所需的所有详细信息

    内容使用 URL 编码确保对您的内容进行 URL 编码。在实际用作分隔符时,请注意 & 和 = 符号的编码。

    更多细节可以在这篇文章中找到: Can BigCommerce Private Apps use OAuth

    【讨论】:

    • 在我的 bigcommerce 演示商店中,控制台响应中出现此错误 [{status: 401, message: "The specified credentials are invalid."}] 0: {status: 401, message: "The specified credentials无效。"}你知道什么是问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 2021-10-14
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多