该请求缺少身份验证密钥。请参考 IHttpClientFactory 部分 "Authentication"
The request was missing an Authentication Key. Please, refer to section "Authentication" with IHttpClientFactory
我正在尝试从 .NET Core 3.1 REST API 向另一个 API 发送一个 HTTP 请求,但在下面继续出现异常尽管我已经添加了身份验证密钥:
请求缺少身份验证密钥。请参阅“身份验证”部分
这是我的代码:
public class AppService
{
private readonly IHttpClientFactory _clientFactory;
string key = "key=llllll ......";
string webAPIKey = "......";
string Id = "......";
public AppService(IHttpClientFactory clientFactory) {
_clientFactory = clientFactory;
}
public async Task<string> sendBroadCastNotification()
{
Data _data = new Data();
_data.title = "test data Ttile";
_data.detail = "test data detail";
Notification _notification = new Notification();
_notification.title = "test notification Ttile";
_notification.body = "test notification body";
MyNotification NotifcationData = new MyNotification {
data=_data,
notification=_notification,
to= "/topics/all"
};
try {
var client = _clientFactory.CreateClient();
client.DefaultRequestHeaders.Add("project_id", Id );
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", key);
StringContent bodyOfRequest = new StringContent(System.Text.Json.JsonSerializer.Serialize(NotifcationData ), Encoding.UTF8, "application/json");
using var httpResponse =
await client.PostAsync("https://.......", bodyOfRequest);
var result = httpResponse.Content.ReadAsStringAsync().Result;
return result;
}
catch (Exception ex)
{
throw new System.ArgumentException(ex.Message.ToString(), "original");
}
//return null;
}
}
我在this link这里找到了答案:
我使用以下行添加密钥:
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", key);
我尝试添加授权 header 的以下方式无法理解为授权 header :
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", key);
我正在尝试从 .NET Core 3.1 REST API 向另一个 API 发送一个 HTTP 请求,但在下面继续出现异常尽管我已经添加了身份验证密钥:
请求缺少身份验证密钥。请参阅“身份验证”部分
这是我的代码:
public class AppService
{
private readonly IHttpClientFactory _clientFactory;
string key = "key=llllll ......";
string webAPIKey = "......";
string Id = "......";
public AppService(IHttpClientFactory clientFactory) {
_clientFactory = clientFactory;
}
public async Task<string> sendBroadCastNotification()
{
Data _data = new Data();
_data.title = "test data Ttile";
_data.detail = "test data detail";
Notification _notification = new Notification();
_notification.title = "test notification Ttile";
_notification.body = "test notification body";
MyNotification NotifcationData = new MyNotification {
data=_data,
notification=_notification,
to= "/topics/all"
};
try {
var client = _clientFactory.CreateClient();
client.DefaultRequestHeaders.Add("project_id", Id );
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", key);
StringContent bodyOfRequest = new StringContent(System.Text.Json.JsonSerializer.Serialize(NotifcationData ), Encoding.UTF8, "application/json");
using var httpResponse =
await client.PostAsync("https://.......", bodyOfRequest);
var result = httpResponse.Content.ReadAsStringAsync().Result;
return result;
}
catch (Exception ex)
{
throw new System.ArgumentException(ex.Message.ToString(), "original");
}
//return null;
}
}
我在this link这里找到了答案:
我使用以下行添加密钥:
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", key);
我尝试添加授权 header 的以下方式无法理解为授权 header :
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", key);