WordPress CPL 使用 JWT 令牌返回错误



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

错误:

解析值时遇到意外字符:<。路径",第 0 行,位置 0。

错误是模糊的,因为所有地狱,我不知道这里的问题是什么。

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

这是我可以找到的关于请求JWToken任务的内容

[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:UsersRoysourcereposEcombetaEcombetaEcombetaViewsLogin.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;
}

它最终变成了非常愚蠢的东西

如果您注意到公共静态字符串 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");
}

最新更新