Full authentication is required to access this resource Unauthorized

来源:互联网 发布:linux配置两个tomcat 编辑:程序博客网 时间:2024/06/06 13:07

在于某银行app对接用户时,对方接口提供了获取accesstoken,方式是通过post进行提交数据,都是基于oauth协议来完成。

例如接口地址:https://test_Id:test_secret@test.com/oauth/authorize

再拿到接口后,肯定要测试一下是否能获取结果,所以就使用了在线post提交请求,模拟获取数据,结果能够成功,想着应该不会有问题,就开发写代码。

但是通过代码时无法获取数据,一直返回401状态,Unauthorized,错误消息:Full authentication is required to access this resource。

不管如何设置,包括请求头部、编码、json、form表单提交等,都是一样的错误,但是在浏览器里模拟就成功了。

返回错误消息:

{"timestamp":1510558350024,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/oauth/authorize"}

怎么解决呢?

我们先看一下给的连接地址,此url明显有2个符合(:  @),查询了一下,发现其实就是一个账户和密码登陆,而我们在浏览器里操作,浏览器默认会自动解析登陆,那代码如何写呢?

解决办法:

                   var tokenRequestParams = new SortedDictionary<string, string>                    {                        {"type", "authorization"}                    };                    var content = new FormUrlEncodedContent(tokenRequestParams);                    var handler = new HttpClientHandler                    {                        Credentials = new NetworkCredential("test_Id", "test_secret")//账户、密码                    };                    var httpClient = new HttpClient(handler);                    var data =  httpClient.PostAsync("https://test_Id:test_secret@test.com/oauth/authorize", content);

NetworkCredential:可以在网络中提供一个凭证,只有获得该凭证的用户才能访问相应的服务的权限,关键就是在这个地方。






阅读全文
0 0