We are using a Woocommerce Webhook to send a JSON request to a client who is using WSO2, but if they switch on authentication I get 401 error.
I have tried setting the consumer key and secret with wp_remote_post() and used the username and password and add it in the header for the request. These are more less the same thing but this is what I tried:
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);
This is the response I receive:
[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
)
)
Related
I have problem with Owncloud. After creating a new user, email sended to that user was with private IP address, not with domain name. Example verification link:
http://192.168.3.103:4490/index.php/index.php/settings/users/setpassword/form/728851077702434409735/some.username
I would like to repair that. It need to be in format
https://cloud.mydomain.pl:4490/index.php/index.php/settings/users/setpassword/form/728851077702434409735/some.username
Owncloud config below
$CONFIG = array (
'instanceid' => 'xxx',
'passwordsalt' => 'xxx',
'secret' => 'xxx',
'trusted_domains' =>
array (
1 => 'mydomain',
2 => 'cloud',
3 => 'https://cloud.mydomain.pl:44900',
4 => '192.168.3.103:4490',
),
'datadirectory' => '/var/www/html/data',
'overwrite.cli.url' => 'https://cloud.mydomain.pl:44900',
'overwritecondaddr' => '^192\\.168\\.3\\.103$',
'dbtype' => 'sqlite3',
'version' => '10.6.0.0',
'logtimezone' => 'UTC',
'installed' => true,
'loglevel' => 0,
// LOGGING
// "log_type" => "owncloud",
// "logfile" => "owncloud.log",
// "loglevel" => "0",
// "logdateformat" => "F d, Y H:i:s",
// "mail_smtpdebug" => true;
'maintenance' => false,
'theme' => '',
'mail_domain' => 'mydomain.pl',
'mail_from_address' => 'admin',
'mail_smtpmode' => 'smtp',
'mail_smtpsecure' => 'ssl',
'mail_smtpauth' => 1,
'mail_smtpauthtype' => 'LOGIN',
'mail_smtphost' => 'mydomain.pl',
'mail_smtpport' => '465',
'mail_smtpname' => 'admin#mydomain.pl',
'mail_smtppassword' => 'xxx',
);
Right after 'overwrite.cli.url' add:
'overwritehost' => 'cloud.mydomain.pl',
I write a code which share a post on linkedin account. My code is working fine for text post but facing issue in image post. I tried and search a lot but not find any success for now. Here is my code for image share in linkedin V2 api.
I follow this doc
https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin?context=linkedin/consumer/context
/*1.Register your image to be uploaded.*/
$imageData = array (
'registerUploadRequest' =>
array (
'recipes' =>
array (
0 => 'urn:li:digitalmediaRecipe:feedshare-image',
),
'owner' => 'urn:li:person:'.$data['identifier'],
'serviceRelationships' =>
array (
0 =>
array (
'relationshipType' => 'OWNER',
'identifier' => 'urn:li:userGeneratedContent',
),
),
),
);
$headers = [
'Content-Type' => 'application/json',
'x-li-format' => 'json',
'X-Restli-Protocol-Version' => '2.0.0',
];
$image_request = $adapter->apiRequest('assets?action=registerUpload', 'POST', $imagedata, $headers);
$image_request = json_decode(json_encode($image_request), True);
/*2.Upload your image to LinkedIn.*/
$media = $image_request['value']['asset'];
$image_path = '/var/www/domain.com/img/laptop-green-bg.jpg';
$postfield = array("upload-file" => $image_path );
$headers = array();
$headers[] = 'Authorization: Bearer '.$tokens['access_token'];// token generated above code
$headers[] = 'X-Restli-Protocol-Version: 2.0.0';
$headers[] = 'Content-Type: data/binary';
$headers[] = 'Content-Length: 0';
$ch = curl_init();
$options = array(
CURLOPT_HEADER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $image_request['value']['uploadMechanism']['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['uploadUrl'],
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_SAFE_UPLOAD => false,
CURLOPT_POSTFIELDS => $postfield
);
curl_setopt_array($ch, $options);
$imgResponse = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$assets = explode(":", $media);
$assetRequest = $adapter->apiRequest('assets/'.$assets[3], 'GET');
/*3. Create the image share.*/
$status = $this->imagePostArray($data, $media);
function imagePostArray($data, $media) {
$newData = array (
'author' => 'urn:li:person:'.$data['identifier'],
'lifecycleState' => 'PUBLISHED',
'specificContent' =>
array (
'com.linkedin.ugc.ShareContent' =>
array (
'shareCommentary' =>
array (
'text' => $data['introtext'],
),
'shareMediaCategory' => 'IMAGE',
'media' =>
array (
0 =>
array (
'status' => 'READY',
'description' =>
array (
'text' => $data['introtext'],
),
'media' => $media,
'title' =>
array (
'text' => $data['introtext'],
),
),
),
),
),
'visibility' =>
array (
'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC',
),
);
return $newData;
}
$response = $adapter->apiRequest('ugcPosts', 'POST', $status, $headers);
print_r($response);
/*responsestdClass Object
(
[id] => urn:li:share:XX4665961029465XXXX
)*/
print_r($imgResponse);
/*HTTP/1.1 201 Created
Date: Tue Jun 18 08:15:02 UTC 2019
Server: Play
Set-Cookie: lang=v=2&lang=en-us; Path=/; Domain=api.linkedin.com
x-ambry-creation-time: Tue Jun 18 08:15:02 UTC 2019
access-control-allow-origin: https://www.linkedin.com
Content-Length: 0
X-Li-Fabric: prod-lor1
Connection: keep-alive
X-Li-Pop: prod-esv5
X-LI-Proto: http/1.1
X-LI-UUID: z1rSbeU8qRUA8kkBZSsXXX==
Set-Cookie: lidc="b=OB77:g=1398:u=7:i=1560845701:t=1560926538:s=AQG2sbwmHWudXf8tikgpzQdf4uhbXXX"
X-LI-Route-Key: "b=OB77:g=1398:u=7:i=1560845701:t=1560926538:s=AQG2sbwmHWudXf8tikgpzQdf4uhbXXX"*/
But still cannot see my post in linkedin. Please help to debug or provide some solution.
I've solved posting using Guzzle library of php. It's simple and straight forward.
First we need to upload the image using following code:
$linkedInClient = new GuzzleHttp\Client(['base_uri' => 'https://api.linkedin.com']);
$response = $linkedInClient->post(
'/media/upload', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer {accessToken}',
],
'multipart' => [
[
'name' => 'fileupload',
'contents' => fopen('image-path', 'r'),
],
],
]
);
After that we need to decode the json response the uploaded image to use in the post request as follow:
$contents = json_decode($response->getBody()->getContents());
Now, prepare the data for linkedin post:
$data = array (
'author' => 'author-id',
'lifecycleState' => 'PUBLISHED',
'specificContent' =>
array (
'com.linkedin.ugc.ShareContent' =>
array (
'media' =>
array (
0 =>
array (
'media' => $contents->location,
'status' => 'READY'
),
),
'shareCommentary' =>
array (
'attributes' => [],
'text' => 'Some Comments',
),
'shareMediaCategory' => 'IMAGE',
),
),
'visibility' =>
array (
'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC',
),
);
Next, we can use this data to post in linkedin as below:
$linkedInClient->post("/ugcPosts", $data);
I hope this helps. We can see the post in the linkedin. However, in my case the post will be visible but the image only gets displayed after some time of upload. But you can see the image on popup after clicking the blank image block.
Thanks.
Work For Me
$curl = curl_init(); //CURL version: 7.29, PHP version: 7.4.26
$imageData = array (
'registerUploadRequest' =>
array (
'recipes' =>
array (
0 => 'urn:li:digitalmediaRecipe:feedshare-image',
),
'owner' => 'urn:li:person:'.$linkedin_id,
'serviceRelationships' =>
array (
0 =>
array (
'relationshipType' => 'OWNER',
'identifier' => 'urn:li:userGeneratedContent',
),
),
),
);
$image_request = json_encode($imageData);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.linkedin.com/v2/assets?action=registerUpload',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_TIMEOUT => 300,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $image_request,
CURLOPT_HTTPHEADER => array('content-type: application/json', "Accept: application/json",
"Authorization: Bearer ".$access_token)
));
$response = json_decode(curl_exec($curl),true);
echo "<pre>";
print_r($response);
Trying to make a test cURL-less Paypal payment request but getting an error.
I am using the Sample request from the paypal Payment API docs.
$data = array (
0 =>
array (
'op' => 'replace',
'path' => '/transactions/0/amount',
'value' =>
array (
'total' => '18.37',
'currency' => 'EUR',
'details' =>
array (
'subtotal' => '13.37',
'shipping' => '5.00',
),
),
),
1 =>
array (
'op' => 'add',
'path' => '/transactions/0/item_list/shipping_address',
'value' =>
array (
'recipient_name' => 'Anna Gruneberg',
'line1' => 'Kathwarinenhof 1',
'city' => 'Flensburg',
'postal_code' => '24939',
'country_code' => 'DE',
),
),
);
$url = 'https://api.sandbox.paypal.com/v1/payments/payment';
$options = array(
'http' => array(
'header' => array(
"Content-type: application/json",
"Authorization: Bearer $access_token"
),
'method' => 'POST',
'content' => http_build_query($data)
)
);
print_r($options);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Return: Warning: file_get_contents(https://api.sandbox.paypal.com/v1/payments/payment): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /storage/ssd5/910/7954910/public_html/paypal.php on line 48
bool(false)
Add
'protocol_version' => '1.1'
to the 'http' => array().
e.g. Change the line
'method' => 'POST',
to
'protocol_version' => '1.1',
'method' => 'POST',
This changes the HTTP protocol used by file_get_contents() from HTTP/1.0 to HTTP/1.1, and HTTP/1.1 is required by the PayPal REST API server.
I am trying to post data using Guzzle 6,
The request which I am generating dynamically looks as below:
$postRequest = Array(
'headers' => array(
['x-api-key'] => 'srDxd39M2FQxxvfvxxcIohcLfKDcdcRUU'
)
'form_params' => array(
[0] => Array (
['name'] => 'function_key'
['contents'] => 'REGISTER'
)
[1] => Array (
['name'] => 'email'
['contents'] => 'tester#test.com'
)
[2] => Array (
['name'] => 'password'
['contents'] => 'test'
)
[3] => Array (
['name'] => 'name'
['contents'] => 'tester'
)
[4] => Array (
['name'] => 'is_org'
['contents'] => 'N'
)
)
)
// Sending Request using 'POST' Method
$client = new GuzzleHttp\Client();
$response = $client->request('POST','abcdxyz.com',$getRequest);
Now my problem is, The Response which I am receiving after sending the above request says "API 'function_key' is missing".
But I am sending 'function_key' as part of request, then What I am missing here ?? Why it is not able to find 'function_key'
Any help is Appreciated
TIA
Try formating it as follows:
$postRequest = [
'headers' => [
'x-api-key' => 'srDxd39M2FQxxvfvxxcIohcLfKDcdcRUU'
],
'form_params' => [
'function_key' => 'REGISTER',
'email' => 'tester#test.com',
'password' => 'test',
'name' => 'tester',
'is_org' => 'N'
]
]
[UPDATE BELOW]
I'm trying to get sdk_token which needed in the native app in this way
$requestParams = array(
'access_code' => $this->PayfortAccessCode ,
'amount' => floatval($amount)*100,
'currency' => strtoupper($currency),
'customer_email' => $email,
'merchant_reference' => $order_id,
'order_description' => $order_description,
'language' => $this->PayfortLanguage,
'merchant_identifier' => $this->PayfortMerchantID,
'payment_option' => $payment_option,
'command' => 'SDK_TOKEN',
'return_url' => $this->PayfortReturnUrl,
'customer_name' => $customer_name,
'customer_ip' => ''//$this->CI->input->ip_address()
);
$filtered_params = array_filter($requestParams);
ksort($filtered_params);
$this->setEncryptionKey($this->PayfortRequestEncryptionKey);
$postData = array();
foreach ($filtered_params as $name => $value) {
$postData[strtolower(htmlentities($name))] = htmlentities($value);
}
$postData['signature'] = $this->generate_signature($filtered_params);
$requestParams=json_encode($parameters['post_data']);
$result = file_get_contents('https://paymentservices.payfort.com/FortAPI/paymentApi', null, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/json' . "\r\n"
. 'Content-Length: ' . strlen($requestParams) . "\r\n",
'content' => $requestParams,
),
)
));
$result=json_decode($result);
print_r($result);
Code is divided into multiple files but I view it as one file here. The problem is I get this response with "Invalid command" error
stdClass Object
(
[amount] => 2130
[response_code] => 00004
[signature] => signature_here
[merchant_identifier] => id_here
[access_code] => code_here
[language] => en
[command] => SDK_TOKEN
[response_message] => Invalid command
[merchant_reference] => reference_here
[customer_email] => email_here
[return_url] => url_here
[currency] => currency_symbol_here
[customer_name] => customer_name_here
[status] => 00
)
So how to fix this to get the SDK_TOKEN
[UPDATE]
changed Command to service_command and now I get this error
Channel not configured for selected payment option
To add new services or commands, I have to call the support to do this.