Azurry
Azurry

Reputation: 526

WordPress CPL using JWT Token return error

I have to validate the users through 2 processes. This is the first: If a valid token is returned I move on to phase 2, which is just pulling a Customers List and then correlating the username. To get the customer's ID etc. and then save it to the local storage to persist the user and allow for Simpler Order Send outs.

The Error:

Unexpected character encountered while parsing value: <. Path '', line 0, position 0.'

The Error is vague as all hell And I have no idea what the problem is here.

  class WpApiCredentials
    {
        public static string SiteUri = "http://TheSite.co.za/";
        public static string WordPressUri = $"https://public-api.wordpress.com/wp/v2/sites/{SiteUri}/";
        public static string Username = "Name";
        public static string Password = "password";
    }


 WpApiCredentials.Username = Usernamelabel.Text;
 WpApiCredentials.Password = PasswordLabel.Text;


var client = new WordPressClient(WpApiCredentials.SiteUri);
client.AuthMethod = AuthMethod.JWT;
await client.RequestJWToken(WpApiCredentials.Username, WpApiCredentials.Password);

       
 var isValidToken = await client.IsValidJWToken();

 if (isValidToken)
            {

             await App.Current.MainPage.DisplayAlert("Token Received", "Phase 1 is done ", "OK");
             Login_Phase2();
                   
            }
            else
            {
                await App.Current.MainPage.DisplayAlert("Empty Values", "Token not Found", "OK");
            }

Here's what I can find in regards to the RequestJWToken Task

        [AsyncStateMachine(typeof(<RequestJWToken>d__29))]
        public Task RequestJWToken(string Username, string Password);

[Obsolete("Use JWT instead of Basic")]
        Basic,

        /// <summary>
        /// JSON Web Token Authentication method. Need configure your site with this plugin https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/
        /// </summary>
        JWT

The Full Error

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.12-02 14:59:18.531 I/mono-stdout(20452):   at Newtonsoft.Json.JsonTextReader.ParseValue () [0x002b3] in <2073514815234917a5e8f91b0b239405>:0 

  at Newtonsoft.Json.JsonTextReader.ParseValue () [0x002b3] in <2073514815234917a5e8f91b0b239405>:0 
  at Newtonsoft.Json.JsonTextReader.Read () [0x0004c] in <2073514815234917a5e8f91b0b239405>:0 
  at Newtonsoft.Json.JsonReader.ReadAndMoveToContent () [0x00000] in <2073514815234917a5e8f91b0b239405>:0 12-02 14:59:18.531 I/mono-stdout(20452):   at Newtonsoft.Json.JsonTextReader.Read () [0x0004c] in <2073514815234917a5e8f91b0b239405>:0 
  at Newtonsoft.Json.JsonReader.ReadForType (Newtonsoft.Json.Serialization.JsonContract contract, System.Boolean hasConverter) [0x0004a] in <2073514815234917a5e8f91b0b239405>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in <2073514815234917a5e8f91b0b239405>:0 
  at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00054] in <2073514815234917a5e8f91b0b239405>:0 
  at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <2073514815234917a5e8f91b0b239405>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in <2073514815234917a5e8f91b0b239405>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSeriali

12-02 14:59:18.531 I/mono-stdout(20452):   at Newtonsoft.Json.JsonReader.ReadAndMoveToContent () [0x00000] in <2073514815234917a5e8f91b0b239405>:0 zerSettings settings) [0x00000] in <2073514815234917a5e8f91b0b239405>:0 
  at WordPressPCL.Utility.HttpHelper.PostRequest[TClass] (System.String route, System.Net.Http.HttpContent postBody, System.Boolean isAuthRequired) [0x00212] in <5cf5507fc1ef4ced882a5bb2a8a2f7af>:0 
  at WordPressPCL.WordPressClient.RequestJWToken (System.String Username, System.String Password) [0x000ba] in <5cf5507fc1ef4ced882a5bb2a8a2f7af>:0 
  at Ecombeta.Views.Login.Login_Phase1 () [0x000a1] in C:\Users\Roy\source\repos\Ecombeta\Ecombeta\Ecombeta\Views\Login.xaml.cs:100 

The Repo Link

https://github.com/wp-net/WordPressPCL/tree/master/WordPressPCL/Client

Found the Method


public async Task RequestJWToken(string Username, string Password)
        {
            var route = $"{_jwtPath}token";
            var formContent = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("username", Username),
                    new KeyValuePair<string, string>("password", Password)
                });

            (JWTUser jwtUser, HttpResponseMessage response) = await _httpHelper.PostRequest<JWTUser>(route, formContent, false).ConfigureAwait(false);
            //JWToken = jwtUser?.Token;
            _httpHelper.JWToken = jwtUser?.Token;
        }

Upvotes: 1

Views: 1391

Answers (1)

Azurry
Azurry

Reputation: 526

It Ended up being something Extremely Stupid

If you notice public static string SiteUri = "http://TheSite.co.za/"; Doesnt have /wp-json at the end.

So Method ended up being

something like this


 var client = new WordPressClient("http://Your-Site/wp-json/");
            client.AuthMethod = AuthMethod.JWT;
            await client.RequestJWToken(TheUserName, ThePassword);

            var x = client;
            var isValidToken = await client.IsValidJWToken();



            WpApiCredentials.token = client.GetToken();

            if (isValidToken)
            {

                Login_Phase2();
            }
            else
            {

                await App.Current.MainPage.DisplayAlert("Empty Values", "Token not Found", "OK");
            }

Upvotes: 1

Related Questions