框 oauth2:无效的 grant_type 参数或参数丢失

问题描述 投票:0回答:6

我不知道我做错了什么,但每次我尝试获取令牌(当然是在用户身份验证之后),结果总是 Invalid grant_typeparameter orparametermissing

可能与 Box API 在获取访问令牌时始终返回无效的 grant_type 参数相关

这是我的小提琴结果:

POST https://api.box.com/oauth2/token HTTP/1.1
Host: api.box.com
Content-Length: 157
Expect: 100-continue
Connection: Keep-Alive

grant_type=authorization_code&code=nnqtYcoik7cjtHQYyn3Af8uk4LG3rYYh&client_id=[myclientId]&client_secret=[mysecret]

结果:

HTTP/1.1 400 Bad Request
Server: nginx
Date: Thu, 07 Mar 2013 11:18:36 GMT
Content-Type: application/json
Connection: keep-alive
Set-Cookie: box_visitor_id=5138778bf12a01.27393131; expires=Fri, 07-Mar-2014 11:18:35 GMT; path=/; domain=.box.com
Set-Cookie: country_code=US; expires=Mon, 06-May-2013 11:18:36 GMT; path=/
Cache-Control: no-store
Content-Length: 99

{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}

即使遵循curl示例也会给出相同的错误。任何帮助将不胜感激。

编辑:尝试使用额外的redirect_uri参数,但仍然是相同的错误

POST https://api.box.com/oauth2/token HTTP/1.1
Content-Type: application/json; charset=UTF-8
Host: api.box.com
Content-Length: 187
Expect: 100-continue
Connection: Keep-Alive

grant_type=authorization_code&code=R3JxS7UPm8Gjc0y7YLj9qxifdzBYzLOZ&client_id=*****&client_secret=*****&redirect_uri=http://localhost

结果:

HTTP/1.1 400 Bad Request
Server: nginx
Date: Sat, 09 Mar 2013 00:46:38 GMT
Content-Type: application/json
Connection: keep-alive
Set-Cookie: box_visitor_id=513a866ec5cfe0.48604831; expires=Sun, 09-Mar-2014 00:46:38 GMT; path=/; domain=.box.com
Set-Cookie: country_code=US; expires=Wed, 08-May-2013 00:46:38 GMT; path=/
Cache-Control: no-store
Content-Length: 99

{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}
oauth-2.0 box-api
6个回答
19
投票

看起来 Box 除了对参数进行正确的 URL 编码之外还需要正确的

Content-Type: application/x-www-form-urlencoded
请求标头。这似乎同样适用于刷新和撤销请求。

此外,根据 RFC 6749

redirect_uri

必需,如果授权请求中包含“redirect_uri”参数 如第 4.1.1 节所述,它们的值必须相同。


4
投票

我也面临着类似的问题。

  • 问题不在于 Content-Type。
  • 问题在于您收到的代码的生命周期。

大多数地方没有提到的一个关键方面是,您在重定向上获得的代码仅持续30秒

要获取访问令牌和刷新令牌,您必须在 30 秒或更短的时间内发出 post 请求。

如果您不这样做,您会收到所述错误。我在这里找到了信息。

下面的代码对我有用。请记住 30 秒规则。

import requests

url = 'https://api.box.com/oauth2/token'

data = [
    ('grant_type', 'authorization_code'),
    ('client_id', 'YOUR_CLIENT_ID'),
    ('client_secret', 'YOUR_CLIENT_SECRET'),
    ('code', 'XXXXXX'),
]

response = requests.post(url, data=data)

print(response.content)

希望有帮助。


1
投票

您缺少重定向 URI 参数。尝试:

POST https://api.box.com/oauth2/token HTTP/1.1
Host: api.box.com
Content-Length: 157
Expect: 100-continue
Connection: Keep-Alive

grant_type=authorization_code&code=nnqtYcoik7cjtHQYyn3Af8uk4LG3rYYh&client_id=[myclientId]&client_secret=[mysecret]&redirect_uri=[your-redirect-uri]

0
投票

我在实施 oauth2 时也面临同样的问题。我已经添加了

Content-Type: application/x-www-form-urlencoded
。当我添加
content-type
时,我的问题就解决了。

检查并添加有效的

content-type


0
投票

不确定谁将来可能需要这个,但请确保您正在发送 POST 请求来获取访问令牌,而不是尝试使用 GET 来检索它,或者如果您正在测试 - 粘贴到地址栏中将不起作用,您需要发送一个 POST 请求,其中的数据在 BODY 中,而不是作为查询参数。

而且该代码通常会持续几秒钟,因此您需要在发回后立即使用它。


0
投票

尝试所有解决方案后,如果错误仍然相同,请在您的

server/app/index
中尝试这个中间件:

app.use(express.urlencoded({ extended: true }));
© www.soinside.com 2019 - 2024. All rights reserved.