如何使用outh与Guzzle 5(或更好,与Guzzle 6) [英] How to use oAuth with Guzzle 5 (or, better, with Guzzle 6)

查看:903
本文介绍了如何使用outh与Guzzle 5(或更好,与Guzzle 6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Guzzle 5连接到WooCommerce API(Guzzle 6似乎没有oAuth选项o.O)。 Woocommerce 需要oAuth身份验证方法才能正常工作。



这是我使用的代码:

 <?php 

/ **
*使用Guzzle 5从WooCommerce商店获取信息
*的示例。
* /

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

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

$ consumer_key ='my_consumer_key'; //在这里添加您自己的Consumer Key
$ consumer_secret ='my_consumer_secret'; // Add your own Consumer Secret here
$ store_url ='http://example.com'; //将主网址添加到您要连接到此处的商店
$ 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...'

a


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


使用纯卷曲功能(使用这个包,其中我提供了一些这里功能


> b

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

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

似乎有些事情在创建签名时有所不同:通过我一直使用的库工作,但创建的通过oAuth插件(使用方法 getSignature() )对于Guzzle不,我不是那么习惯使用oAuth来查找错误。是否有人可以帮助我识别问题?

解决方案

更新@Andndir答案



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


在我的例子中,编辑,因为我试图连接到
WooCommerce API版本2,但该版本的API没有实现
正确的OAuth核心1.0a规范。事实上,他们在
中修复了API的版本3中的这个问题。查看V3和旧版
版本之间的差异。



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


为了让他的答案正常工作,我们需要使用 wc-api / v3 / api / v2 / 。



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

 使用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();


I'm trying to connect to the WooCommerce API using Guzzle 5 (Guzzle 6 seems not has oAuth options o.O). Woocommerce requires the oAuth authentication method to work.

This is the code I'm using:

<?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"...'

This code returns a

woocommerce_api_authentication_error: Invalid Signature - provided signature does not match

Using pure curl functions (using this package in which I've put some functions I found here), instead, it works and I get all orders and other data I want.

SOME OTHER DETAILS

To use Guzzle 5 and oAuth I use the those composer packages:

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

It seems there are some things that are different in creating the signature: the one created by the library I've used until now works, but the one created by the oAuth plugin (using the method getSignature()) for Guzzle doesn't and I'm not so used to use oAuth to find the error. Is there someone who can help me identify the problem?

解决方案

Updating @Aerendir answer

In his pull request, @Aerendir wrote:

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

So, to make his answer work properly, we need to use wc-api/v3/ instead of wc-api/v2/.

The following code, works using Guzzle 6, oauth and 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();

这篇关于如何使用outh与Guzzle 5(或更好,与Guzzle 6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆