If you are building a native Android app for your e-commerce store, connecting it securely to your backend is a crucial first step. In this guide, we will walk you through the exact steps to authenticate and verify WooCommerce in your Android application using the WooCommerce REST API.
Step 1: Configure WooCommerce Settings (Server-Side)
Before touching your Android code, you need to generate the necessary API keys from your WordPress dashboard.
- Log in to WordPress: Access your admin dashboard.
- Navigate to Settings: On the left-hand menu, go to WooCommerce > Settings.
- Enable the REST API: Click on the Advanced tab at the top, then click on the REST API link right below it.
- Create a New Key: Click the Add key button.
- Configure Key Details:
Description: Add a friendly name for your app (e.g., “Android App API”).
User: Select the admin user generating the key.
Permissions: Change this to Read/Write so your app can both fetch products (GET) and submit orders (POST).
Generate: Click Generate API Key.
Important: WooCommerce will now display a Consumer Key and a Consumer Secret. Copy these immediately and keep them secure, as you will need them for your Android project.
Step 2: Configure Android App Settings (Client-Side)
Now that your server is ready, let’s set up the Android app to communicate with WooCommerce.
- Add the OAuth Library: Add an OAuth library (like ScribeJava) to your Android project. The modern way to do this is by adding the dependency to your
build.gradlefile, though you can also add the.jarfile manually to yourapp/libsfolder. - Store Your Keys: Open your
strings.xmlfile and add the API keys you generated earlier:
<string name="woo_consumer_key">YOUR_CONSUMER_KEY</string>
<string name="woo_consumer_secret">YOUR_CONSUMER_SECRET</string> 3. Create an OAuth Service: Use your Consumer Key and Consumer Secret to initialize an OAuth service that will request an OAuth token for authorization.
4. Construct OAuth Requests: Build your requests based on the action you want to perform:
- For GET methods: Use this to fetch data like products or categories.
- For POST methods: Use this to push data, such as creating a new customer or submitting a new order. Ensure you include a
JSONObjectcontaining all required parameter values in the body of your request.
5. Send Signed Requests: Construct a signed request for the OAuth service using an async HTTP client method, and send it to your server.
Step 3: Fetch Products from Your WooCommerce Store
Once authentication is set up, you can start pulling catalog data into your app.
To list products belonging to specific categories, you can call the Products API and append the category IDs to the URL. If a parent category has subcategories, simply pass the IDs of all the subcategories in the URL to fetch all relevant products.
Example API Call:
https://your_wordpress_site/wp-json/wc/v3/products?category=26,27,28 Understanding the Product Response: In the JSON response, every product includes a categories array detailing exactly where the product belongs. You can use the category name or ID to filter products dynamically within your app’s UI.
"categories": [
{
"id": 26,
"name": "Subcategory",
"slug": "subcategory"
}
] Pro Tip: When displaying a subcategory screen in your app, use this JSON array to filter the master list so that users only see products relevant to the specific subcategory they are browsing.