React Native Woocommerce API fails when parameters are set - react-native

I recently decided to give React Native a shot. I am running into some trouble communicative with Woocommerce using the react-native-woocommerce-api.
When I get all the products, it works great, but when I try to set an attribute to the HTTP call, it fails.
This works:
componentDidMount() {
console.log("Loading products...");
Woocommerce.get("products", {}).then(response => {
console.log(response);
});
}
This doesn't (per_page added):
componentDidMount() {
console.log("Loading products...");
Woocommerce.get("products", {per_page: 3}).then(response => {
console.log(response);
});
}
Error thrown:
Object {
"code": "woocommerce_rest_authentication_error",
"data": Object {
"status": 401,
},
"message": "Invalid signature - the signature doesn't match.",
}
HTTP request that works:
http://www.example.com/wp-json/wc/v3/products?oauth_consumer_key=###&oauth_nonce=###&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1560818379&oauth_version=1.0&oauth_signature=###=&
HTTP request that doesn't work:
http://www.example.com/wp-json/wc/v3/products?oauth_consumer_key=###&oauth_nonce=###&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1560817909&oauth_version=1.0&oauth_signature=###=&per_page=3
I should also add that when adding per_page=3 using Postman, it works. I don't know what difference it makes, the HTTP calls are very similar, it seems that only the uri parameters order are different.
Any help is appreciated! Been stuck on this the whole freaking day. :/

Open react-native-woocommerce-api.js in node_modules\react-native-woocommerce-api\lib\
then goto line 195 (_getOAuth().authorize function) and change:
params.qs = this._getOAuth().authorize({
url: url,
method: method
});
to
params.qs = this._getOAuth().authorize({
url: url + '?' + this.join(data, '&'),
method: method
});

Related

Nuxt3 and Cloudinary Upload API: The "path" argument must be of type string. Received an instance of Object

I am trying to use the Cloudinary Upload API to programmatically upload a File object to my Cloudinary media library, but for some reason I get the following server error:
{
"url": "/api/cloudinary/upload",
"statusCode": 500,
"statusMessage": "Internal Server Error",
"message": "The \"path\" argument must be of type string. Received an instance of Object",
"description": "<pre><span class=\"stack internal\">at new NodeError (node:internal/errors:371:5)</span>\n<span class=\"stack internal\">at validateString (node:internal/validators:119:11)</span>\n<span class=\"stack\">at basename (node:path:752:5)</span>\n<span class=\"stack internal\">at post // snipped out rest
Am I not setting up the folder path correctly here or is something else the problem?
I am using the following code (in my NuxtJS app):
components/UserProfileAvatar.vue:
const file = ref(null)
const handleAvatarUpload = async (e) => {
file.value = e.target.files[0]
// Upload file to Cloudinary and get public_id
const options = {
public_id: 'profile-image',
folder: `users/${userStore.userProfile.uid}/avatar/`
}
try {
const response = await $fetch('/api/cloudinary/upload', {
method: 'POST',
body: {
options: options,
file: file.value
}
})
console.log('Uploaded!', response.asset)
} catch (error) {
console.log('Error uploading file', error)
}
}
If I console log the logic above, I get the following output:
Anyone spot anything as to why I get the the error "The Path argument must be of type string. Received an instance of Object" in my original attempt?

How to return ajax response with error http code from PrestaShop ModuleFrontController?

I start working with PrestaShop 1.7.6 (before I worked with Symfony) and I write few custom modules to PrestaShop, but now I want send json data from my front controller module to user. Everything works fine if I send json with http code 200, but now I want send error message with proper http code (e.g 400). In Symfony I can do that by using JsonResponse (I try to do that here but it's not working as expected).I saw in presta controller are just two methods with ajax response (ajaxDie - which is deprecated and ajaxRender), but both of them doesn't takes as parameter http code response and always send 200.
if (!$product) {
$json = Tools::jsonEncode(['status' => false]);
$this->ajaxRender($json);
//return new JsonResponse($json, Response::HTTP_BAD_REQUEST);// doesn't send proper code
}
Can anyone tell me how to send error code from module front controller which extends ModuleFrontController?? Now only possible to me action is send error message with http code 200 (but I think it's bad idea to send error with that code). Thanks a lot for any help.
The proper way to send and receive data using AJAX is displayed below.
More information can be found at Prestashop Docs
Ajax post
$.ajax({
type: 'POST',
dataType : 'json',
url: 'linktoyourfile.php',
data: { action: 'getMyrequest' },
success: function(data) {
alert(data.result);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error: ' + textStatus + ' ' + errorThrown);
}
});
Module Front Controler
class TestModuleFrontController extends ModuleFrontController
{
public function init()
{
parent::init();
}
public function displayAjaxGetMyrequest()
{
// ERROR HANDELING
if ($this->errors) {
die(Tools::jsonEncode(array('hasError' => true, 'errors' => $this->errors)));
} else {
echo Tools::json_encode(array('result' => 'test result'));
}
}
}

vuejs2 get return network error

I am trying to access a prestashop API with vuejs2
<script>
import axios from 'axios'
export default {
data () {
return {
get: [],
errors: []
}
},
created () {
axios({
method: 'get',
url: 'https://myprestashopsite.com/api/categories/?ws_key=J***************Z&filter[id_parent]=5&output_format=JSON'
}).then(response => {
this.get = response.data
})
.catch(e => {
this.errors.push(e)
})
}
}
In the web developer console of mozilla I see that my request return a 200 with the data in the response. But I get "Error: Network Error app.js%20line%201266%20%3E%20eval:15:15" catch as an error.
I tried with another API and it worked, so I guess it comes from the prestashop api. (prestashop version 1.7.3.0)
Is there a way to fix this ?
The problem seems to come from axios. I had to add a rule to the server.
I found the solution to this on this thread :
https://github.com/axios/axios/issues/853
There are other solutions I didn't try in this thread if mine doesn't work.
how to add the rule : https://enable-cors.org/server.html

Using NodeJS for our API, how to control what HTTP status codes are sent from Hapi?

We are using Hapi for our simple API. We have out own auth system, so if a user calls our API, but they are not authenticated, I want to send back a 401 HTTP status code.
I saw this post:
https://futurestud.io/tutorials/hapi-how-to-set-response-status-code
which offers this example:
handler: function (request, reply) {
var data = { key: 'value' }
reply(data).code(201)
}
So I started adding in response codes, including codes for our exceptions:
DB.knex.raw(query).then(function(result) {
var dataJson = [];
var responseJson = {};
for(var i in result[0]) {
dataJson.push({
"name": result[0][i]["name"],
"profile_id": result[0][i]["profile_id"],
"profile_type": result[0][i]["profile_type"],
"profile_classification": result[0][i]["profile_classification"],
"permalink": result[0][i]["permalink"],
"location": result[0][i]["location"],
});
}
responseJson["data"] = dataJson;
reply(responseJson).code(200);
})
.catch(function(e) {
console.error(e);
reply(e).code(500);
});
but I immediately got this error:
TypeError: query.then(...).catch(...).code is not a function
So... I guess I can't do this. I am confused by the error, since I am not calling ".code()" on the "catch", I am calling it on the reply().
We are using Boom, does that override the advice in the article? The article talks about Boom, but does not make clear that Boom overrides the earlier advice.

Unable to test Open Graph

I'm trying to create my own actions/objects. When checking with the debugger everything is fine, but when testing with my dev account I get this in my callback:
{"data":[]}
Is it good or not because I see anything in my timeline, news feed or ticker.
When testing with a test user I get this error:
{
"error": {
"message": "Call to a member function on a non-object",
"type":"BadMethodCallException"
}
}
What's wrong?
You are supposed to make POST request to facebook api something like below and make sure you have initialised FB:
var opts = {
animal : OBJECT_URL,
access_token: token
};
FB.api('/me/[MY_APP_NAMESPACE]:hunt', 'post', opts, function(response)
{
});