我们正在使用Woocommerce Webhook向使用WSO2的客户端发送JSON请求,但是如果他们打开身份验证,则会收到401错误。
我尝试使用wp_remote_post()设置使用者密钥和机密,并使用了用户名和密码并将其添加到请求的标头中。这些不是同一件事,但这是我尝试过的:
token = array( 'key' => 'MY_KEY','secret' => 'MY_SECRET', 'username' => 'MY_USERNAME', 'password' => 'MY_PASSWORD', 'default');
$response = wp_remote_post('URL', array(
'headers' => array(
'username' => 'MY_USERNAME',
'password' => 'MY_PASWORD',
'Authorization' => 'basic ' . $token,
'key' => 'CONSUMER KEY',
'secret' => 'SECRET',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'www-authenticate' => array( 'key' => 'CONSUMER KEY','secret' => 'CONSUMER KEY', 'username' => 'USERNAME', 'password' => 'PASSWORD', 'default'),));
$basicauth = 'Basic ' . base64_encode( 'USERNAME' . ':' . 'PASSWORD' );
$headers = array(
'Authorization' => $basicauth,
'Content-type' => 'application/json',
'Content-length' => $contentlen
);
$pload = array(
'method' => 'POST',
'timeout' => 30,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => $headers,
'body' => $bdy,
'cookies' => array()
);
$response = wp_remote_post('URL', $pload);
这是我收到的回复:
[Response] => Array
(
[Code] => 401
[Message] => Unauthorized
[Headers] => Requests_Utility_CaseInsensitiveDictionary Object
(
[data:protected] => Array
(
[date] => Tue, 23 Oct 2018 06:43:03 GMT
[server] => server
[accept] => */*
[access-control-allow-origin] => *
[access-control-allow-methods] => POST
[www-authenticate] => OAuth2 realm="WSO2 API Manager", error="invalid token", error_description="The access token expired"
[x-wc-webhook-source] => https://site . com/
[referer] => URL
[x-forwarded-host] => host
[x-forwarded-proto] => https
[x-wc-webhook-topic] => order.created
[x-wc-webhook-signature] => SIGNATURE
[accept-encoding] => deflate, gzip
[x-wc-webhook-event] => created
[access-control-allow-headers] => authorization,Access-Control-Allow-Origin,Content-Type,SOAPAction
[x-wc-webhook-delivery-id] => ID
[expect] => 100-continue
[x-wc-webhook-id] => 1
[x-forwarded-for] => Array
(
[0] => test
[1] => test
)
[x-wc-webhook-resource] => order
[x-forwarded-server] => server
[content-type] => application/xml; charset=UTF-8
)
)