【问题标题】:How to use oAuth with Guzzle 5 (or, better, with Guzzle 6)如何将 oAuth 与 Guzzle 5 一起使用(或者更好的是与 Guzzle 6 一起使用)
【发布时间】:2015-10-06 00:12:24
【问题描述】:

我正在尝试使用 Guzzle 5 连接到 WooCommerce API(Guzzle 6 似乎没有 oAuth 选项 o.O)。 Woocommerce requires the oAuth authentication method 工作。

这是我正在使用的代码:

<?php

/**
 * Example of usage of Guzzle 5 to get information
 * from a WooCommerce Store.
 */

require('../vendor/autoload.php');

use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Oauth\Oauth1;
use GuzzleHttp\Exception\RequestException;

$consumer_key = 'my_consumer_key'; // Add your own Consumer Key here
$consumer_secret = 'my_consumer_secret'; // Add your own Consumer Secret here
$store_url = 'http://example.com'; // Add the home URL to the store you want to connect to here
$api_path = '/wc-api/v2/';
$api_end_point = [
    'root' => '',
    'orders' => 'orders'
    ];

$base_uri = $store_url . $api_path;

$client = new Client([
    'base_url' => $base_uri,
    'defaults' => ['auth' => 'oauth']
    ]);

$oauth = new Oauth1([
    'consumer_key'    => $consumer_key,
    'consumer_secret' => $consumer_secret,
    'request_method'  => 'query'
]);

$client->getEmitter()->attach($oauth);

try
{
    $res = $client->get($api_end_point['orders']);
}
catch (RequestException $e)
{
    $res = $e;

    if ($e->hasResponse())
    {
        $res = $e->getResponse();
    }
}

print_r($res);

echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'

此代码返回一个

woocommerce_api_authentication_error:无效签名 - 提供 签名不匹配

使用纯 curl 函数(使用 this package,我在其中添加了一些我发现 here 的函数),它可以工作,我得到所有订单和我想要的其他数据。

其他一些细节

要使用 Guzzle 5 和 oAuth,我使用了这些作曲家包:

"require": {
    "guzzlehttp/guzzle": "~5.0"
},
"require-dev": {
    "guzzlehttp/oauth-subscriber": "~0.2",
},

似乎在创建签名时有些不同之处:the library I've used until now 创建的签名有效,但 Guzzle 的 oAuth 插件 (using the method getSignature()) 创建的签名无效,我不是这样曾经使用 oAuth 来查找错误。有人可以帮我找出问题吗?

【问题讨论】:

    标签: php curl oauth guzzle


    【解决方案1】:

    更新@Aerendir 答案

    @Aerendir 在他的拉取请求中写道:

    就我而言,我在尝试连接到 WooCommerce API 版本 2,但该版本的 API 未实现 正确的 OAuth Core 1.0a 规范。事实上,他们在 API 的第 3 版。请参阅 V3 和旧版本之间的差异 版本。

    来源:https://github.com/guzzle/oauth-subscriber/pull/42#issuecomment-185631670

    所以,为了让他的答案正常工作,我们需要使用 wc-api/v3/ 而不是 wc-api/v2/.

    以下代码使用 Guzzle 6、oauth 和 WooCommerce api v3:

    use GuzzleHttp\Client,
        GuzzleHttp\HandlerStack,
        GuzzleHttp\Handler\CurlHandler,
        GuzzleHttp\Subscriber\Oauth\Oauth1;
    
    $url = 'http://localhost/WooCommerce/';
    $api = 'wc-api/v3/';
    $endpoint = 'orders';
    $consumer_key = 'ck_999ffa6b1be3f38058ed83e5786ac133e8c0bc60';
    $consumer_secret = 'cs_8f6c96c56a7281203c2ff35d71e5c4f9b70e9704';
    
    $handler = new CurlHandler();
    $stack = HandlerStack::create($handler);
    
    $middleware = new Oauth1([
        'consumer_key'    => $consumer_key,
        'consumer_secret' => $consumer_secret,
        'token_secret'    => '',
        'token'           => '',
        'request_method' => Oauth1::REQUEST_METHOD_QUERY,
        'signature_method' => Oauth1::SIGNATURE_METHOD_HMAC
    ]);
    $stack->push($middleware);
    
    $client = new Client([
        'base_uri' => $url . $api,
        'handler' => $stack
    ]);
    
    $response = $client->get( $endpoint, [ 'auth' => 'oauth' ] );
    echo $response->getStatusCode() . '<br>';
    echo $response->getHeaderLine('content-type') . '<br>';
    echo $response->getBody();
    

    【讨论】:

      【解决方案2】:

      现在插件OauthSubscriber 仅适用于 Guzzle 6。 再次测试,我发现了错误:它在方法 signUsingHmacSha1() 中无论如何都会在要签名的字符串中添加一个与号 (&),这会导致来自 WooCommerce 的错误。

      我已经在 GitHub 上 opened a issuesent a pull request 修复错误。

      使用 Guzzle 6 连接到 WooCommerce API V2 的正确方法(修复错误后!注意您连接的 WooCommerce API 版本:API v3 仍然没有'不工作!)是这样的:

      use GuzzleHttp\Client;
      use GuzzleHttp\HandlerStack;
      use GuzzleHttp\Handler\CurlHandler;
      use GuzzleHttp\Subscriber\Oauth\Oauth1;
      
      $options = array(
          // Add the home URL to the store you want to connect to here (without the end / )
          'remoteUrl'          => 'http://example.com/',
          // Add your own Consumer Key here
          'remoteConsumerKey'  => 'ck_4rdyourConsumerKey8ik',
          // Add your own Secret Key here
          'remoteSecretKey'    => 'cs_738youconsumersecret94i',
          // Add the endpoint base path
          'remoteApiPath' => 'wc-api/v2/',
      );
      
      $remoteApiUrl = $options['remoteUrl'] . $options['remoteApiPath'];
      $endpoint = 'orders';
      
      $handler = new CurlHandler();
      $stack = HandlerStack::create($handler);
      
      $middleware = new Oauth1([
          'consumer_key'    => $options['remoteConsumerKey'],
          'consumer_secret' => $options['remoteSecretKey'],
          'token_secret'    => '',
          'token'           => '',
          'request_method' => Oauth1::REQUEST_METHOD_QUERY,
          'signature_method' => Oauth1::SIGNATURE_METHOD_HMAC
      ]);
      $stack->push($middleware);
      
      $client = new Client([
          'base_uri' => $remoteApiUrl,
          'handler' => $stack
      ]);
      
      $res = $client->get($endpoint, ['auth' => 'oauth');
      

      如前所述,此连接仅适用于 WooCommerce API 版本 2。

      我正在调查了解为什么 V3 不起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-20
        • 1970-01-01
        • 2019-08-26
        • 2017-10-11
        • 2019-01-21
        • 1970-01-01
        相关资源
        最近更新 更多