TObase64String 未对我的访问令牌进行编码,导致 401 未经授权
TObase64String not encoding my Access token , resulting to 401 unauthorized
问题是 ToBase64string 中的字符串 'resulted' 在调试时正在转换,但是当我在编码后使用结果时,似乎在我想要的 'var response =' 中编码后使用'resulted',它不基于调试模式进行编码。为什么?我是不是漏了什么
[HttpGet, Route("values/get")]
public async Task<string> Get(string resulted)
{
//resulted.Remove(0, 17);
string res = "";
using (var client = new HttpClient())
{
// HTTP POST
client.BaseAddress = new Uri("https://api.elliemae.com/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(resulted)));
var response = client.GetAsync("/encompass/v1/loans/{ea7c29a6-ee08-4816-99d2-fbcc7d15731d}?Authorization=Bearer "+resulted+"&Content-Type=application/json").Result;
using (HttpContent content = response.Content)
{
// ... Read the string.
Task<string> result = content.ReadAsStringAsync();
res = result.Result;
}
}
return res;
}
您需要在请求中添加授权 header。您需要将 Authorization header 设置为 Bearer [acces_token]
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer "+ access_token);
注意 Bearer 单词后有一个 space。
这个值 new AuthenticationHeaderValue("Bearer "+ access_token) 应该类似于 new AuthenticationHeaderValue("Bearer asadasda23qdaddfs45345") 其中 asadasda23qdaddfs45345 是您收到的访问令牌。
当您的 Acesstoken 返回额外数据时,例如:“{\"access_token\":\"uKW7HeksFXz5QE1sF6Kjmfda5Fxi\",\"token_type\":\"Bearer\" }\r\n"
然后使用 'substring' 将其放入 accesstoken 代码中,例如应该如下所示 -> 'uKW7HeksFXz5QE1sF6Kjmfda5Fxi'
怎么样? Accesstoken.Substring(x, x); (x=你从字符串中减去的一些数字)
然后将其放入您的 DefaultRequestHeaders
public async Task<string> Get(string Accesstoken)
{
string res = "";
using (var client = new HttpClient())
{
Accesstoken = Accesstoken.Substring(17, 28);
client.BaseAddress = new Uri("https://api.elliemae.com/");
//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Accesstoken);
var response = client.GetAsync("encompass/v1/loans/ea7c29a6-ee08-4816-99d2-fbcc7d15731d").Result;
using (HttpContent content = response.Content)
{
// ... Read the string.
Task<string> result = content.ReadAsStringAsync();
res = result.Result;
}
问题是 ToBase64string 中的字符串 'resulted' 在调试时正在转换,但是当我在编码后使用结果时,似乎在我想要的 'var response =' 中编码后使用'resulted',它不基于调试模式进行编码。为什么?我是不是漏了什么
[HttpGet, Route("values/get")]
public async Task<string> Get(string resulted)
{
//resulted.Remove(0, 17);
string res = "";
using (var client = new HttpClient())
{
// HTTP POST
client.BaseAddress = new Uri("https://api.elliemae.com/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(resulted)));
var response = client.GetAsync("/encompass/v1/loans/{ea7c29a6-ee08-4816-99d2-fbcc7d15731d}?Authorization=Bearer "+resulted+"&Content-Type=application/json").Result;
using (HttpContent content = response.Content)
{
// ... Read the string.
Task<string> result = content.ReadAsStringAsync();
res = result.Result;
}
}
return res;
}
您需要在请求中添加授权 header。您需要将 Authorization header 设置为 Bearer [acces_token]
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer "+ access_token);
注意 Bearer 单词后有一个 space。
这个值 new AuthenticationHeaderValue("Bearer "+ access_token) 应该类似于 new AuthenticationHeaderValue("Bearer asadasda23qdaddfs45345") 其中 asadasda23qdaddfs45345 是您收到的访问令牌。
当您的 Acesstoken 返回额外数据时,例如:“{\"access_token\":\"uKW7HeksFXz5QE1sF6Kjmfda5Fxi\",\"token_type\":\"Bearer\" }\r\n" 然后使用 'substring' 将其放入 accesstoken 代码中,例如应该如下所示 -> 'uKW7HeksFXz5QE1sF6Kjmfda5Fxi'
怎么样? Accesstoken.Substring(x, x); (x=你从字符串中减去的一些数字)
然后将其放入您的 DefaultRequestHeaders
public async Task<string> Get(string Accesstoken)
{
string res = "";
using (var client = new HttpClient())
{
Accesstoken = Accesstoken.Substring(17, 28);
client.BaseAddress = new Uri("https://api.elliemae.com/");
//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Accesstoken);
var response = client.GetAsync("encompass/v1/loans/ea7c29a6-ee08-4816-99d2-fbcc7d15731d").Result;
using (HttpContent content = response.Content)
{
// ... Read the string.
Task<string> result = content.ReadAsStringAsync();
res = result.Result;
}