o使用 Open API 的身份验证 2
本文关键字:身份验证 API 使用 Open | 更新日期: 2023-09-27 18:33:55
我正在尝试将控制台应用程序授权给使用 oAuth 2.0 的 API。我尝试过DotNetOpenAuth,但没有成功。
第一步是获取授权代码,这就是我所拥有的,但我找不到授权代码以继续执行步骤 2(使用预定义的授权代码进行授权,带有用户 ID 和用户密码)。我使用了文档中的以下授权标头https://sandbox-connect.spotware.com/draftref/GetStartAccounts.html#accounts?
在响应中,我没有收到任何带有身份验证代码的标头,我该如何解决这个问题?
string sAuthUri = "https://sandbox-connect.spotware.com/oauth/v2/auth";
string postData ="access_type=online&approval_prompt=auto&client_id=7_5az7pj935owsss8kgokcco84wc8osk0g0gksow0ow4s4ocwwgc&redirect_uri=http%3A%2F%2Fwww.spotware.com%2F&response_type=code&scope=accounts";
string sTokenUri = "https://sandbox-connect.spotware.com/oauth/v2/token";
var webRequest = (HttpWebRequest)WebRequest.Create(sAuthUri);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
try
{
using (Stream s = webRequest.GetRequestStream())
{
using (StreamWriter sw = new StreamWriter(s))
sw.Write(postData.ToString());
}
using (WebResponse webResponse = webRequest.GetResponse())
{
using (var reader = new StreamReader(webResponse.GetResponseStream()))
{
response = reader.ReadToEnd();
}
}
var return = response;
}
catch (Exception ex)
{
}
使用 OAuth2 的身份验证代码流,代码应位于重定向到 redirect_uri 的查询字符串中。
我认为问题出在调用方法(应该是 GET),postData 作为 QueryString,redirect_uri必须是系统的 url(不是 spotware)。应从 webResponse 的查询字符串中检索授权代码。
我建议您使用DotNetOpenAuth库轻松实现OAuth2请求。