【问题标题】:Bigcommerce Auth Callback /w PHP APIBigcommerce 身份验证回调/w PHP API
【发布时间】:2015-02-25 06:16:08
【问题描述】:

使用这个 bigcommerce Auth 回调(新 API 的一部分)让我大开眼界。 我已经尝试过常规的 PHP API 和在这里找到的最新分支 https://github.com/maetl/bigcommerce-api-php/tree/f43e652c71550f9074dbf696c740b30651110051,它应该支持 OAUTH。

我在这里使用 php 下的https://developer.bigcommerce.com/apps/callback 的演示代码开始。我已经正确设置了我的应用程序等但是我的 php 没有那个错误。下面是使用链接的 OAUTH 分支,因为原始具有此代码尝试调用的不存在的功能。

这是我的代码...

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include $_SERVER['DOCUMENT_ROOT'] . '/libraries/bigcommerce/bigcommerce.php';

use Bigcommerce\Api\Connection;

$tokenUrl = "https://login.bigcommerce.com/oauth2/token";
$connection = new Connection();
$connection->useUrlencoded();
$response = $connection->post($tokenUrl, array(
    "client_id" => "#myid",
    "client_secret" => "#mysecret",
    "redirect_url" => "http://#myoauthurl",
    "grant_type" => "authorization_code",
    "code" => $request->get("code"),
    "scope" => $request->get("scope"),
    "context" => $request->get("context"),
));

$token = $response->access_token;
?>

这首先抱怨数组最后 3 部分的“get”,并出现这样的错误......

Notice: Undefined variable: request in /var/www/integrations/bigcommerce/oauth2.php on line 18 Fatal error: Call to a member function get() on a non-object in /var/www/integrations/bigcommerce/oauth2.php on line 18

要删除那些我手动添加的...

"code" => $_GET["code"],
"scope" => $_GET["scope"],
"context" => $_GET["context"],

通过这些更改,我得到以下错误。

Fatal error: Uncaught exception 'Bigcommerce\Api\NetworkError' with message 'failed setting cipher list' in /var/www/libraries/bigcommerce/bigcommerce.php:95 Stack trace: #0 /var/www/libraries/bigcommerce/bigcommerce.php(169): Bigcommerce\Api\Connection->handleResponse() #1 /var/www/integrations/bigcommerce/oauth.php(21): Bigcommerce\Api\Connection->post('https://login.b...', Array) #2 {main} thrown in /var/www/libraries/bigcommerce/bigcommerce.php on line 95

上面对 handleReponse() 的引用似乎与库中的 CURL 操作有关。 它还指密码列表,这是以前的问题,需要

Bigcommerce::setCipher('RC4-SHA');
Bigcommerce::verifyPeer(false);

我可以在此处附加正确的类以获取它们,尽管我似乎没有为这部分设置密码,因为它继续呻吟。

还有其他人在上述方面有进一步的运气或有其他想法吗?

【问题讨论】:

    标签: php curl bigcommerce


    【解决方案1】:

    最终使用 CURL 来实现这一点,而不是让 API 变得更加困难。

    如下...

    $data = array(
        "client_id" => "",
        "client_secret" => "",
        "redirect_uri" => "http://",
        "grant_type" => "authorization_code",
        "code" => $_GET["code"],
        "scope" => $_GET["scope"],
        "context" => $_GET["context"],
    );
    $postfields = http_build_query($data);
    
    $ch = curl_init();                    
    $url = "https://login.bigcommerce.com/oauth2/token";
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);
    curl_close ($ch);
    
    $obj = json_decode($output);
    $access_token = $obj->{'access_token'};
    $scope = $obj->{'scope'};
    $id =  $obj->{'user'}->{'id'};
    $email =  $obj->{'user'}->{'email'};
    $context = $obj->{'context'};
    

    【讨论】:

    • 您好,我知道这篇文章已经发布了几个月,但我一直在查看 BigCommerce OAuth 文档,我对第一步感到非常困惑。我过去使用过 OAuth,并且我了解在您获得授权码后该怎么做(交换令牌、发出请求等),但您最初应该在哪里引导您的用户登录?
    • 你没有。他们登录 Bigcommerce 并点击“应用程序”,然后 OAUTH 进入您的应用程序。即使用他们的 Bigcommerce 登录进入您的网站。这有意义吗?
    • 哦,我明白了。所以这不是您在 Bigcommerce 之外可以拥有的东西吗?我在想这是您可以集成到独立应用程序中的东西,但他们必须先登录到他们的 bigcommerce 帐户,然后才能访问您的应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 2018-08-07
    相关资源
    最近更新 更多