无法从 Thinktecture 授权服务器获取令牌

Unable to get token from Thinktecture Authorization Server

我无法从 Thinktecture 授权服务器获取访问令牌。成功获得授权代码后,我尝试向令牌端点发出 POST 请求,但总是收到 400 Bad Request 和此响应: 消息:“{"error":"invalid_client"}”

我的要求是: POST 到 https://host/authz/users/oauth/token 请求正文: {"code":"grant_code_received_from_previous_request","client_id":"myclient","grant_type":"authorization_code","client_secret":"mysecret"}

我的客户端已在授权服务器中正确设置。我的客户 ID 和密码是正确的;它们与我在上一个请求中用于授予代码的值相同 (/users/oauth/authorize)。

对这个 "invalid_client" 问题有什么想法吗?除了 "invalid_client".

之外,响应中没有其他信息

您可以获得有关日志文件的更多信息。 看一下,可以查看Identity Server的web.config上的logging选项:

 <system.diagnostics>
<!-- simple system.diagnostics logger -->
<trace autoflush="true" indentsize="4">
  <listeners>
    <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="Trace.log" />
    <remove name="Default" />
  </listeners>
</trace>

<!-- can be used with TraceSource logger -->
<!--<sources>
  <source name="Thinktecture.IdentityServer"
          switchValue="Information, ActivityTracing">
    <listeners>
      <add name="xml"
           type="System.Diagnostics.XmlWriterTraceListener"
           initializeData= "trace.svclog" />
    </listeners>
  </source>
</sources>-->

有关登录的更多信息,请访问: http://identityserver.github.io/Documentation/docs/configuration/logging.html

您需要使用 http 基本身份验证发送客户端凭据,而不是在正文中发布 ID 和密码。

我听从了@leastprivilege 的建议并做到了:

// set up the base64-encoded credentials
let clientId = "myclientid"
let clientSecret = "myclientsecret"

let loginString = NSString(format: "%@:%@", clientId, clientSecret)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(0))

之后像这样创建了请求实例:

var request = NSMutableURLRequest(URL: myUrl!)
request.HTTPMethod = "POST"

// Only these four are required based on the documentation.        
let postString = "grant_type=authorization_code&code=\(code)&redirect_uri=app:/your.redirect.uri"

request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")

希望对您有所帮助!