我正在尝试在 REST API 上注册新用户,请求应该是这样的:
POST /api/oauth/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: Basic YW75cm9pZC12tb28Jp8bGU6c2Vj
grant_type=client_credentials&scope=write
问题 1:授权码是静态的,每次我请求新用户时都必须相同,还是每次都应该生成一个新的 base46 码?
问题2:grant_type....是一个参数请求还是一个body(我应该把它和body一起发送还是作为参数发送)
非常感谢!
最佳答案
首先,根据您提供的请求代码:
POST /api/oauth/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: Basic YW75cm9pZC12tb28Jp8bGU6c2Vj
grant_type=client_credentials&scope=write
在我看来,请求是为了获取客户端(应用程序)访问 token (使用 OAuth Client Credentials Grant
),而不是请求注册一个用户。所以,我的回答基于这个假设。
Question 1: the Authorization code is static it must be the same every time I request a new user or I should generate a new base46 code every time?
在 OAuth Client Credentials Grant
的情况下,Authorization
header 包含客户端身份验证信息(例如,client_id
和 client_secret
)。您通常通过向 API 提供商注册您的客户端(应用程序)来获取此信息。
所以答案是,是的,只要信息(client_id/secret)不改变/过期,它就应该是静态的。
Question 2: The grant_type.... is a parameter request or a body ( shall I send it with the body or as a parameter)
OAuth spec
规定grant_type
参数写在HTTP请求entity-body中,所以应该是 em> 在请求正文中发送。但是,某些实现也允许在 url 中传递 grant_type
参数。
关于javascript - 我如何使用 "grant_type=client_credentials"发送 HTTP header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39118944/