代码之家  ›  专栏  ›  技术社区  ›  Simant

在三条腿OAuth2.0流中未接收访问令牌asp.netmvc(黑板学习)

  •  0
  • Simant  · 技术社区  · 6 年前

    https://community.blackboard.com/docs/DOC-3976-three-legged-oauth

    我已经通过调用restapi/learn/API/public/v1/oauth2收到了授权代码/授权代码。之后根据文档(我严格遵循了文档,但不知道我遗漏了什么),我构建了一个POST请求/learn/api/public/v1/oauth2/token来获取access\u token,但我无法获取access\u token。

    相反,我收到了一个错误的请求。这意味着我在构建第二个请求时犯了一个错误,但我无法修复该问题。我在.NET中还没有找到任何实现黑板学习的三条腿验证的代码示例。你能帮我解决这个问题吗?

    这是我调用这两个API来接收访问令牌的代码。

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            // GET /learn/api/public/v1/oauth2/authorizationcode
    
            Guid stateId = Guid.NewGuid();
    
            string applicationKey = "Application key goes here";
    
            string redirectUrl = string.Format("https://Blackboard Learn URL goes here/learn/api/public/v1/oauth2/authorizationcode" +
    
            "?redirect_uri=https://localhost:44300/Home/OAuth2Response&response_type=code&client_id={0}&scope=read&state={1}",
    
            applicationKey, stateId);
    
            Response.Redirect(redirectUrl, true);
    
            return View();
        }
    
    
        public async Task<bool> OAuth2Response(string code = null, string state = null, string error = null, string error_description = null)    
        {    
            bool success = true;  
    
            string json = string.Empty;
    
            string urlCommand = string.Format("/learn/api/public/v1/oauth2/token?code={0}&redirect_url=https://localhost:44300/Home/OAuth2Response", code);
    
            try    
            {    
                using (HttpClient client = new HttpClient())    
                {    
                    var endpoint = new Uri("Blackboard Learn URL goes here" + urlCommand);
    
    
                    var postData = new List<KeyValuePair<string, string>>();
    
                    postData.Add(new KeyValuePair<string, string>("grant_type", "authorization_code"));    
    
                        HttpContent body = new FormUrlEncodedContent(postData);       
    
                    // POST /learn/api/public/v1/oauth2/token
    
                    using (HttpResponseMessage response = await client.PostAsync(endpoint, body)) // Problem is here    
                    {    
                        if (response.IsSuccessStatusCode)
    
                        {
    
                            json = await response.Content.ReadAsStringAsync();
    
                        }
    
                        else
    
                        {
    
                            success = false;
    
                        }
    
                    }
    
                }
    
            }
    
            catch (Exception err)
    
            {
    
                //hopefully we never end up here, log this exception for forensics
    
                success = false;
    
            }
    
            return success;    
        }    
    }
    

    注意:我可以在邮递员工具中成功接收一个访问令牌。

    0 回复  |  直到 4 年前
        1
  •  1
  •   user-id-14900042    4 年前

    public class HomeController : Controller
    {
        //https://blackboard.jiveon.com/docs/DOC-3976-three-legged-oauth 
    
        public ActionResult Index()
        {
            // GET /learn/api/public/v1/oauth2/authorizationcode
    
            Guid stateId = Guid.NewGuid();
    
            string applicationKey = "Application key goes here";
    
            string redirectUrl = string.Format("Blackboard Learn URL goes here/learn/api/public/v1/oauth2/authorizationcode" +
                "?redirect_uri=https://localhost:44300/Home/OAuth2Response&response_type=code&client_id={0}&scope=read&state={1}",
                applicationKey, stateId);
    
            Response.Redirect(redirectUrl, true);
    
            return View();
        }
    
    
        public async Task<bool> OAuth2Response(string code = null, string state = null, string error = null, string error_description = null)
        {
            bool success = true;
            string json = string.Empty;
            string urlCommand = string.Format("/learn/api/public/v1/oauth2/token?code={0}&redirect_uri=https://localhost:44300/Home/OAuth2Response", code);
    
            try
            {
                using (HttpClient client = new HttpClient())
                {
    
                    var endpoint = new Uri("Blackboard Learn URL goes here" + urlCommand);
                 
    
                    client.DefaultRequestHeaders.Accept.Clear();
    
                    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes("client_id:client_secret")));                  
    
                    var postData = new List<KeyValuePair<string, string>>();
                    postData.Add(new KeyValuePair<string, string>("grant_type", "authorization_code")); 
    
                    HttpContent body = new FormUrlEncodedContent(postData);
    
    
                    using (HttpResponseMessage response = await client.PostAsync(endpoint, body))
                    {
    
                        if (response.IsSuccessStatusCode)
                        {
                            json = await response.Content.ReadAsStringAsync();
    
                            dynamic oauth2Result = Newtonsoft.Json.JsonConvert.DeserializeObject(json);     
    
                            string access_token = oauth2Result.access_token;
    
                            string refresh_token = oauth2Result.refresh_token;           }
                        else
                        {
                            success = false;
                        }
                    }
                }
            }
            catch (Exception err)            {
                //hopefully we never end up here, log this exception for forensics      
                success = false;
            }
            return success;           
        }     
    }
    
    推荐文章