Search code examples
c#wordpressxamlxamarin.formswordpress-rest-api

Xamarin Forms - Best Method for Connecting to Wordpress through API


I'm trying to connect to my Wordpress website using a REST API through my iOS app written in Xamarin Forms.

My workflow is this:

  1. User enters in their username and password in the login screen on the app.

  2. The code will connect to my wordpress website to see if the username / password matches the one in wordpress.

  3. If it matches, then proceed to the next page which lists the user's details.

Here's my code: XAML

<Label x:Name="Login" Text="Login" TextColor="Black"></Label>

<Entry x:Name="LoginUsername" Placeholder="Username *" PlaceholderColor="Gray" />

<Entry x:Name="LoginPassword" Placeholder="Password *" PlaceholderColor="Gray" IsPassword="True" />

<Button x:Name="LoginButton" Text="Login" BackgroundColor="Green" TextColor="White" Clicked="BtnLoginHandler"></Button>

CS

public async Task<Customers> GetAllCustomers()
        {
            var httpClient = new HttpClient();
            string GetAllCustomersApiUrl = string.Format("{0}/wc-api/v3/customers/4/?consumer_key={1}&consumer_secret={2}", website_url, consumer_key, consumer_secret);
            var response = await httpClient.GetAsync(GetAllCustomersApiUrl);
            HttpContent content = response.Content;
            var json = await content.ReadAsStringAsync();
            return customers;
        }

I think i'm missing something during the authentication etc for it doesn't work.

I've looked around online but there's a few solutions that have been deprecated.

What's the best method to retrieve user data from Wordpress successfully?

Any guidance / documentation / code would be useful. Thanks.


Solution

  • Solved the issue. Need to use JWT Auth plugin to help authenticate the username and password.