我需要知道如何从Orange Message Api获取访问令牌。我写了一个应该这样做的函数,但它似乎包含一个bug。它返回一个例外 [远程服务器返回错误400错误请求]。我不知道出了什么问题
class MessageApi
{
public void GetAccessToken()
{
string AuthHeader = "My Authorization header";
HttpWebRequest requeust = (HttpWebRequest)WebRequest.Create("https://api.orange.com/oauth/v2/token");
requeust.ContentType = "application/json";
requeust.Method = "POST";
JsonObject JsonData = new JsonObject();
JsonData["authorization"] = AuthHeader;
JsonData["grant_type"] = "client_credentials";
using (var streamWriter = new StreamWriter(requeust.GetRequestStream()))
{
streamWriter.Write(JsonData);
}
var httpResponse = (HttpWebResponse)requeust.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
}
答案 0 :(得分:0)
Authorization标头应位于请求标头中。
而不是做
JsonData["authorization"] = AuthHeader;
尝试
requeust.Headers["Authorization"] = AuthHeader;
答案 1 :(得分:0)
首先执行此操作: client_id和client_secret用于指定authorization_header。 它的值由关键字“ Basic”,后跟空格和client_id,“:”和client_secret
的串联的base64编码组成。您需要在此站点中将您的client_id:client_secret转换为base64:base64 encode
示例:
client_id = djdv6 + 4fb64b + 6fd + 6b
client_secret = 3464ggb646
已配对= djdv6 + 4fb64b + 6fd + 6b:3464ggb646
djdv6 + 4fb64b + 6fd + 6b:3464ggb646 == d64b6b464b6g4bg65465gn6n ==
执行此操作之后:
curl -X POST -H "Authorization: Basic myBase64Encode" -d "grant_type=client_credentials" https://api.orange.com/oauth/v2/token
将myBase64Encode替换为在基数64中转换的级联clientId和clientSecret ==> d64b6b464b6g4bg65465gn6n ==