尝试从用户获取YouTube访问令牌时出错{错误:“ unsupported_grant_type”,错误说明:“无效的grant_type:”}

时间:2020-04-28 19:53:33

标签: ruby-on-rails ruby api oauth-2.0 youtube

我实际上是在使用红宝石(“ yt”)来获得用户访问其YouTube帐户的许可。到目前为止一切顺利,如此处https://developers.google.com/youtube/v3/guides/auth/server-side-web-apps?hl=es所示,我没有任何问题地进入了步骤3,并获得了类似于4 / adsfaisdofjaosifjosdifjsdoajifsado的代码(您得到了这个主意,很多随机字符)。

文档然后说我需要发出如下所示的POST请求,才能使用该代码获取实际的访问令牌:

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf&
client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&
client_secret=hDBmMRhz7eJRsM9Z2q1oFBSe&
redirect_uri=http://localhost/oauth2callback&
grant_type=authorization_code

是的。我在控制台中使用curl -d以及通过传递所有要求的信息的javascript使用fetch进行了尝试。我的JavaScript代码如下:

 fetch("https://accounts.google.com/o/oauth2/token", {
    method: "POST",
    headers: {
      'Host': 'accounts.google.com',
      "Content-Type": "application/x-www-form-urlencoded"},
    body: {
                "code": '4/2asdasdasdasd',
                "client_id": 'myclientid.apps.googleusercontent.com',
                "client_secret": 'myclientsecret',
                "grant_type": "authorization_code",
                "redirect_uri": 'https://myuri'
              }
  })
  .then((response) => response.json())
  .then((data) => console.log(data))

我得到的是POST https://accounts.google.com/o/oauth2/token 400错误,返回的JSON是这样:

{error: "unsupported_grant_type", error_description: "Invalid grant_type: "}

有人对发生的事情有任何了解吗?

0 个答案:

没有答案