How to get woocommerce cart item using REST API? - reactjs

I'm new to WordPress & Woocommerce and have been trying to get cart content using cocart's for a website running on a different domain to sync the cart between a Woocommerce website and a React Application
I've added some products in the cart from the shop website and calling the "mydomain.com/wp-json/cocart/v1/get-cart" REST API from Postman but getting an empty array in the response.
From their doc, they mentioned a Cart Key and Cookie: wp_cocart_session_, but I'm confused on how to get the Cart Key.
Any kind of help will be highly appreciated.
Thanks in advance

use this plugin
https://wordpress.org/plugins/cart-rest-api-for-woocommerce/
or you can save the product id in sqlite and fetch data from woocommerce rest api to every product in the sqlite database and count the total

I had the same problem. Cookies apparently need to be sent with the request as well as the cart_key.
I solved this by installing the Postman Interceptor browser extension.
In Postman, click "Cookies" and it should ask if you want to setup Interceptor for the domain that you are requesting from. It will install a browser extension and get the cookies and add them to your Postman requests.
Postman provides an assisted step-by-step setup of Interceptor and cookie capture, but to manually access cookie capture settings click the icon in the upper right of Postman that says "Capture requests and cookies with Postman". That's where those setting are.
More about Interceptor here: https://learning.postman.com/docs/sending-requests/capturing-request-data/interceptor/#installing-interceptor

Related

Setting woocommerce cookies headless call

I am attempting to build a headless woocommerce app. Backend is woocommerce, front end is React.
When I query the woocommerce API from insomnia all the required cookies for basket management are coming back, so I am able to get products, add items to cart and get current cart with ease as the cookies are being set properly.
However, when I use axios to call the API I am not getting any cookies through. This means I cant maintain a basket etc.
I tried manually setting cookies as a test but the browser won't allow it, but to be honest I wouldn't want to do that anyway as I need woocommerce to set the cookies to be dynamic.
When I go to the normal wordpress site the cookies are there.
How can I get the cookies to come through when I call it by axios?
If it works in insomnia then it should also work in axios/React?
Many thanks

How can I retrieve a user ID from Firestore via Flask backend (React frontend)?

I'm a newbie with authentication! I'm building a web app where users can log in, and the data shown in the web app is different for each user. I'm using Reactjs as my frontend with Firebase authentication. After a user logs into my web app, I'm storing their user ID (UID) and other information into Firestore. I have a collection usersCollection where each document is labelled with the UID. For the backend, I'm using Flask as mostly a REST API with a Postgres database, but I am not storing user credentials there (UID, password, etc.).
For some of my backend functions I need to change the output based on which user is signed in, but I'm not sure how to retrieve the current user's UID. I'm able to make an axios request to send the current user's UID from the frontend to the backend, so I've tried 2 methods with that:
Saving the axios request output as a global variable - this has led to Flask errors like runtimeerror: working outside of application context. and I don't think this is the best solution.
With each GET request that the frontend is making to the backend (every time there's a function whose output changes based on user), I am passing the UID as a parameter, which causes latency problems.
What is the simplest way for me to request the current UID from Firestore from the backend?
Is structuring our frontend, backend, database, and authentication like this recommended? Or is there a simpler way or better system for our situation (JWT?)? We selected Firebase authentication in the first place because we are using a React MUI template that already set up Firebase for us.
Thank you in advance! Happy to provide more information if needed!
I don't know reactjs, but I have the same setup with flutter (iOS / Android apps).
What I did and what worked out well is:
authenticate your client against firebase (which it looks you already achieved)
extract the idToken from the firebase response
send the idToken to your flask backend, which verifies the id token (see below)
in flask backend, log in the user with login_user() from flask_login. This creates a cookie session which is sent back to the client in the response headers
the reactjs client stores the cookie and needs to attach it to every subsequent API request to flask (this might come out of the box for reactjs, but for flutter I needed some custom code for that)
As for the token validation you can…
use the python sdk
use a jwt library such as pyjwt, see documentation
There is flask-firebase which does a good job for the token validation. I wrote a blog post which gives an example how you would use this.

How can I get media from instagram API?

I'm using the instagram API to get some images related to an specific hastag but when I call the API I get a message saying the API is deprecated.
I'm calling this endpoint
https://api.instagram.com/v1/tags/{tag-name}/media/recent?access_token=ACCESS-TOKEN
Any idea on what call I need to make to return the list of images with the requested hastag?
So I figure out why this is happening. Because the application is still in sandbox mode I can only get hashtag from the accounts that have access. Once the application is approved it will be able to pull the data from the API.

Laravel angularjs cross origin session

I have a problem with communication between angular and laravel.
Its about mobile application that needs to log in to a laravel framework and get some data.
Mobile application is angularjs based, and for login i user standard $httpd function.
Login works fine, and i get expected response from laravel, but when i make another request assuming that im loged in, laravel returns NULL for session and auth. When I upload mobile app to a server where laravel is installed all works like a charm. But my app needs to be on mobile device, so thats not a solution.
I assume that the problem is in cors or cross domain communication. It seems that laravel destroys session made from cross origin requests, because that session is not accessible from another request, and on another request laravel tries to make a new one.
I need help solving that session problem (access session after login or keep session), i hope some of you have some kind of a solution.
i tried :
changing headers in my login controller
changing get to post
adding content type to my angular http req (Content-Type': 'application/x-www-form-urlencoded)
tried requerst with jquery ajax
changed laravels app/config/session.php values of driver, cookie, domain...
setting vendor/laravel/framework/src/illuminate/http/frameGuard.php x-frame options to false
no luck!
Use json web tokens, and simply set it up so that you request a JWT on login, and you can share that token around - the only way it invalidates, is either if you lose the token, or it times out.

get value of access token

I started using google API recently . I am using simpleauth https://github.com/crhym3/simpleauth for authentication to google app engine . Now I am using google blogger API for publishing my blog and fetching data .
This API requires access_token value to use the API https://developers.google.com/blogger/docs/3.0/using#RetrievingPostsForABlog for Authorization . I cant find a way to get value of access token .
Is there a way to get the value of acces token or am I doing something wrong ?
You need to register your webapp with Google to get a client ID and client secret. Then, you can configure your OAuth2 library with these details to allow you to send fully authenticated requests from your webapp to Blogger.
For the specific scenario you listed, retrieving a blog post, I think you can follow step 1 of this page and then follow these steps. You should be able to copy+paste the key from there into the query params of the GET request.
To issue fully authenticated requests, for publishing new posts, for example, you'll have to get your OAuth2 library with the client ID and client secret and have it issue the requests for you.

Resources