gpt4 book ai didi

php - 如何将 oAuth 与 Guzzle 5(或者更好的是,与 Guzzle 6)一起使用

转载 作者:可可西里 更新时间:2023-11-01 00:30:58 29 4
gpt4 key购买 nike

我正在尝试使用 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: Invalid Signature - provided signature does not match

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

一些其他细节

要使用 Guzzle 5 和 oAuth,我使用那些 Composer 包:

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

创建签名时似乎有些不同:the library I've used until now 创建的签名有效,但是由 oAuth 插件 ( using the method getSignature() ) 为 Guzzle 创建的那个无效,而且我不太习惯使用 oAuth 来查找错误。有没有人可以帮我找出问题所在?

最佳答案

更新@Aerendir 回答

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

In my case, I did the editing as I were trying to connect to the WooCommerce API version 2 but that version of the API didn't implement correctly the OAuth Core 1.0a spec. In fact, they fixed this issue in the version 3 of the API. See Differences between V3 and older versions.

source: 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();

关于php - 如何将 oAuth 与 Guzzle 5(或者更好的是,与 Guzzle 6)一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31456066/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com