【问题标题】:WordPress CPL using JWT Token return errorWordPress CPL 使用 JWT 令牌返回错误
【发布时间】:2020-03-26 20:01:48
【问题描述】:

我必须通过 2 个流程验证用户。这是第一个:如果返回了有效的令牌,我将进入第 2 阶段,这只是拉取客户列表,然后关联用户名。获取客户的 ID 等,然后将其保存到本地存储以保留用户并允许更简单的订单发送。

错误:

解析值时遇到意外字符:

错误非常模糊,我不知道问题出在哪里。

  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");
            }

这是我可以找到的关于 RequestJWToken 任务的内容

        [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

完整的错误

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 

回购链接

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

找到方法


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;
        }

【问题讨论】:

  • 这行代码崩溃了,有什么异常?
  • 等待 client.RequestJWToken(WpApiCredentials.Username, WpApiCredentials.Password);
  • 现在也出现此错误“获取响应流时出错 (ReadDoneAsync2): ReceiveFailure”
  • 我猜您正在 client.RequestJWToken() 中进行某种反序列化,如果是这种情况,则此错误可能意味着您收到无效的 JSON 响应,可能是由于来自服务器。如果不是这种情况,您需要在 client.RequestJWToken() 中发布代码,以便我们更好地查看它
  • @Scarnet Ima 更新我所拥有的和我能找到的 导航这个 Wrapper 是可怕的

标签: c# wordpress xamarin jwt xmlhttprequest


【解决方案1】:

它最终变得非常愚蠢

如果您注意到公共静态字符串 SiteUri = "http://TheSite.co.za/";最后没有 /wp-json。

所以方法最终是

类似的东西


 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");
            }

【讨论】:

    猜你喜欢
    • 2018-11-03
    • 2021-01-22
    • 2018-04-07
    • 2018-03-16
    • 2022-07-08
    • 1970-01-01
    • 2021-10-20
    • 2017-11-03
    • 1970-01-01
    相关资源
    最近更新 更多