如何使用c#从DotNetOpenAuth获取Token OAuthAuthorizationServer示例

本文关键字:Token OAuthAuthorizationServer 示例 获取 DotNetOpenAuth 何使用 | 更新日期: 2023-09-27 18:15:32

如何获得Token从DotNetOpenAuth OAuthAuthorizationServer样本与c# ?我正在运行Github中提供的示例。我想要获得代币,暂时不成功。总是得到一个400,糟糕的请求。我发送的请求如下:

var request = WebRequest.CreateHttp("http://localhost:50172/OAuth/Token");
        request.Method = "POST";
        request.ContentLength = 0;
        request.Headers.Add("client_id", "sampleconsumer");
        request.Headers.Add("client_secret", "samplesecret");
        request.Headers.Add("grant_type", "authorization_code");
        request.Headers.Add("code", "teste");
        request.Headers.Add("redirect_uri", "");
        WebResponse response = null;
        try
        {
            response = request.GetResponse();
        }
        catch (Exception ex)
        {
            //400 -  bad request here.
        }

我不懂OAuth,这是我第一次尝试。我已经搜索了很多,但仍然有点困惑

如何使用c#从DotNetOpenAuth获取Token OAuthAuthorizationServer示例

不要为ClientID和其他东西使用标题,将它们放在正文中。

这就是我如何获得Token与客户端凭据授予:

using Newtonsoft.Json;
...
var url = "http://localhost:50172/OAuth/Token"
var request = WebRequest.Create(url);
request.Method = "POST";
string data = "grant_type=client_credentials&client_id=" +
    "sampleconsumer&client_secret=samplesecret";
request.ContentType = "application/x-www-form-urlencoded";
byte[] dataStream = Encoding.UTF8.GetBytes(data);
request.ContentLength = dataStream.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(dataStream, 0, dataStream.Length);
newStream.Close();
WebResponse response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
    string result = reader.ReadToEnd();
    accessToken = JsonConvert.DeserializeObject<AccessToken>(result);
}