【问题标题】:oAuth 2 with Open APIoAuth 2 与开放 API
【发布时间】:2013-10-04 19:24:32
【问题描述】:

我正在尝试将控制台应用程序授权给使用 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)
        {

        }

【问题讨论】:

    标签: c# oauth oauth-2.0


    【解决方案1】:

    使用 OAuth2 的授权代码流,代码应该出现在重定向到 redirect_uri 的查询字符串中。

    我认为问题出在调用方法(应该是 GET)中,postData 为 QueryString,redirect_uri 必须是您系统的 url(不是污点)。授权码应该从 webResponse 的查询字符串中获取。

    我建议您使用 DotNetOpenAuth 库来轻松实现 OAuth2 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-10
      • 2015-10-31
      • 2011-05-06
      • 2020-02-29
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多