Shopware - How to get products using App Credentials? - shopware6

I created App in Shopware and able to get apiKey, secretKey, shopUrl and shopId. I want to get all my products using these credentials.
This is my code
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "http://shopware.example.com/store-api/product",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Content-Type: application/json",
"sw-access-key: SW234C1LVA010CWHDD34WK1TMW"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$data = json_decode($response, true);
print_r($data);
}
and I am getting error like this
Array
(
[errors] => Array
(
[0] => Array
(
[status] => 412
[code] => FRAMEWORK__ROUTING_SALES_CHANNEL_NOT_FOUND
[title] => Precondition Failed
[detail] => No matching sales channel found.
[meta] => Array
(
[parameters] => Array
(
)
)
)
)
)
When I using API key from settings page API I am able to get products.
But how to use App credentials (App key, secret key, shop URL) and get products

You should have gotten the API key during App registration:
https://developer.shopware.com/docs/guides/plugins/apps/app-base-guide#confirmation-request
Did you try that one? If it does not work for the store API you might want to use the admin API.

Related

Add Tracking API in Paypal - Authorization failed

I am trying to add tracking number and order status in my sandbox account through tracking API but getting "Authorization failed due to insufficient permissions" error. Anyone have any idea why this error happens? Below are my codes and response from paypal -
API request
$clientId = 'my client id';
$secretKey = 'my secret key';
$token = $this->getAccessToken($clientId, $secretKey);
$transaction = $this->_objectManager->create('\Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory')->create()->addOrderIdFilter($currentOrder->getId())->getFirstItem();
$transactionId = $transaction->getData('txn_id');
$postfields = [
'trackers' =>
[
0 =>
[
'transaction_id' => $transactionId,
'tracking_number' => $currentOrderTrackingNumber,
'status' => 'SHIPPED',
'carrier' => 'OTHER',
'carrier_name_other' => 'SCS Logistics'
]
]
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api-m.sandbox.paypal.com/v1/shipping/trackers-batch",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($postfields),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer " . $token
),
));
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, true);
Response from paypal
Array
(
[tracker_identifiers] => Array
(
)
[errors] => Array
(
[0] => Array
(
[name] => NOT_AUTHORIZED
[message] => Authorization failed due to insufficient permissions
[details] => Array
(
[0] => Array
(
[field] => /trackers/1/transaction_id
[value] => 42L93730906985049
[location] => body
[issue] => USER_NOT_AUTHORIZED
[description] => You are not authorized to add or modify tracking number for this transaction
)
)
[links] => Array
(
)
)
)
[links] => Array
(
[0] => Array
(
[href] => https://api-m.sandbox.paypal.com/v1/shipping/trackers-batch
[rel] => self
[method] => POST
[encType] => application/json
)
)
)
I already contacted support and they added required permission but it's not working. Please if anyone have any idea about it help me. Thanks in advance.

Upload file using Guzzle 6 to ebay FEED_API endpoint

I'm tryng to upoad e file with ebay FEED_API using Guzzle 6.5 for FILE-EXCHANGE migration to REST API:
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.ebay.com']);
$client->request('POST', '/sell/feed/v1/task/task-40-XXXXXXXXXX/upload_file', [
'headers' => [
'Authorization' => 'Bearer ' . $this->getToken($ebayStore),
'Content-Type' => 'multipart/form-data'
],
'multipart' => [
[
'name' => 'fileName',
'contents' => basename(/path/to/file)
],
[
'name' => 'name',
'contents' => 'file'
],
[
'name' => 'type',
'contents' => 'form-data'
],
[
'name' => 'file',
'contents' => fopen(/path/to/file, 'r'),
'filename' => basename(/path/to/file)
]
]
]);
but i get the following error:
[errors] => Array
(
[0] => stdClass Object
(
[errorId] => 2003
[domain] => ACCESS
[category] => APPLICATION
[message] => Internal error
[longMessage] => There was a problem with an eBay internal system or process. Contact eBay developer support for assistance
[parameters] => Array
(
[0] => stdClass Object
(
[name] => code
[value] => 400
)
[1] => stdClass Object
(
[name] => message
[value] => HTTP 400 Bad Request
)
)
)
)
but if i try to upload the file by cli using curl it works:
curl -g -k -X POST "https://api.ebay.com/sell/feed/v1/task/task-40-XXXXXXXXXX/upload_file" -H "Content-Type: multipart/form-data" -H "Authorization: Bearer v^1.1#i^1#r^0#f^0#p^3#I^3#t..." -i -F "file=#/path/to/file"
It seems api endpoint does not recognize the file associated with the form when i'm using guzzle for the request.
Where am i wrong?
--- EDIT ---
even so it works:
$headers = [
'Authorization:Bearer ' . $this->getToken($ebayStore),
'Content-Type:multipart/form-data'
];
$formData = [
'fileName' => basename($file),
'name' => 'file',
'type' => 'form-data',
'file' => curl_file_create($file)
];
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, self::API_REQUEST_URI.$taskid.'/upload_file');
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($connection, CURLOPT_MAXREDIRS , 10);
curl_setopt($connection, CURLOPT_HEADER, 1);
curl_setopt($connection, CURLINFO_HEADER_OUT, true);
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $formData);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($connection);
curl_close($connection);
My mistake was to manually insert the content-type in the headers.
This is my working code:
My mistake was to manually insert the content-type in the headers.
This is my working code:
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.ebay.com']);
$client->request('POST', '/sell/feed/v1/task/task-40-XXXXXXXXXX/upload_file', [
'headers' => [
'Authorization' => 'Bearer ' . $this->getToken($ebayStore)
],
'multipart' => [
[
'name' => 'file',
'contents' => fopen($file, 'r'),
'filename' => basename($file),
'headers' => [
'Content-Disposition' => 'form-data; name="file"; fileName="'.basename($file).'"; type="form-data"'
]
]
]
]);

PHP cURL as DELETE using POSTFIELDS

In my case the user is logged in and would like to delete his account.
Therfore he triggers a POST form, revalidating the process with a confirmation of his password.
Now I would like to send a cURL DELETE to the api/server and at the same time deliver the confirmed password as well.
My approach is:
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://www.myurl.com/v1/users",
CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode(['cnfpwd' => $_POST['cnfpwd']]), //confirmed password
CURLOPT_HTTPHEADER => array('X-API-KEY: '$apiKey),
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "DELETE",
));
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
//For testing purposes only
print_r($response);
print_r($data);
If this approach is correct, how would I get the POSTFIELDS on the API/server side?
As this is a DELETE request, $_GET, $_POST, $_REQUEST are empty.
EDIT:
The api code for testing purposes:
<?php
if($_SERVER['REQUEST_METHOD'] == "DELETE"){
echo"POST: ";print_r($_POST);
}
?>
The result is:
POST: Array ( )
You should be able to get the body of the request with
$data = file_get_contents('php://input');
My final solution is:
$parameters = file_get_contents("php://input");
$parameters = json_decode($parameters, true);
#Paolos Solution is correct, I just needed to add the second line in order to access the parameters as an Array.

JIRA expand = changelog not working

I'm trying to query jira for data with folowing jql:
$jql_options = array(
"jql" => $jql,
"startAt" => 0,
"maxResults" => 1,
"validateQuery" => false,
"fields" => array(
"summary",
"status",
"assignee"
),
"expand" => "changelog"
);
And i get this error:
string(282) "{"errorMessages":["Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token\n at [Source: org.apache.catalina.connector.CoyoteInputStream#464d8928; line: 1, column: 2] (through reference chain: com.atlassian.jira.rest.v2.search.SearchRequestBean[\"expand\"])"]}"
I understand it's an error with "expand" => "changelog" but on their site they present an requeste representation like mine.
Can u give me some advice? :)
It seems i was reading the rest api documentation wrong.
"expand" option was available only in get request.
I appended jql data in url + "&expand=changelog" and other options i send them in post!
$jql_options = array(
"startAt" => 0,
"maxResults" => 1,
"validateQuery" => false,
"fields" => array(
"key"
)
);
$data = json_encode($jql_options);
$options = array(
CURLOPT_URL => $this->jiraURL . "/rest/api/latest/search?jql=" .urlencode($jql) . "&expand=changelog",
CURLOPT_HTTPHEADER => array(
'Authorization: Basic ' . base64_encode($this->username.":".$this->password),
'Content-type: application/json') ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $data
);

Tracking numbers aren't submitting to fulfillment in orders on Shopify

Working on the fulfillment cycle on some middleware and after beginning to automatically fulfill orders, I noticed that none of the orders have their perspective tracking numbers associated with the fulfillment.
I have the following PHP code submitting the fulfillment request:
$options = array(CURLOPT_URL => 'https://'.$this->config->item('api_key').':'.$this->config->item('api_pass').'#'.$this->config->item('api_domain').'/admin/orders/'.$order['order_id'].'/fulfillments.json',
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_MAXREDIRS => 3,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_USERAGENT => 'HAC',
CURLOPT_CONNECTTIMEOUT =>30,
CURLOPT_TIMEOUT => 30,
CURLOPT_POSTFIELDS => $fulfill_json
);
if($send == true){
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$response_header = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($response_header == 201){
//success
}else{
//error
}
}
Here's an example of the $fulfill_json I'm sending:
{"fulfilment":{"tracking_number":"1Z6V66666666666666","notify_customer":true,"line_items":[{"id":111111111}]}}
{"fulfilment":{"tracking_number":"1Z6V66666666666666","notify_customer":true,"line_items":[{"id":111111111},{"id":111111111},{"id":111111111}]}}
To me it looks like correctly submitted JSON, not sure if there's anything else wrong with it.
First thing I'd try changing is your spelling:
fulfillment not fulfilment