为 SPA 应用程序交换访问令牌的授权代码
Exchange Authorization Code for Access Token for SPA application
我有一个带有 Rails BE 的 SPA React 应用程序。我正在使用 https://github.com/anthonyjgrove/react-google-login 生成带有 accessType 离线的 code
。这会成功生成一个代码,我可以取回它。
现在,我正在尝试使用此代码通过 POSTMAN
向 https://oauth2.googleapis.com/token 发出 POST 请求
curl --location --request POST 'https://oauth2.googleapis.com/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'code=4/0AX4XfWhUc8IC12kr1115g71111' \
--data-urlencode 'redirect_uri=urn:ief:wg:oauth:2.0:oob' \
--data-urlencode 'client_id=x.apps.googleusercontent.com' \
--data-urlencode 'client_secret=secret' \
--data-urlencode 'scope=' \
--data-urlencode 'grant_type=authorization_code'
就使用 https://developers.google.com/oauthplayground 而言,这看起来是正确的。它在这里工作。
但是,当我尝试通过 curl 或 HttParty 发出请求时,它失败了。我不确定是否会发生这种情况,因为我从 SPA 应用程序获取代码,然后使用我的 BE 应用程序发出其他请求。
[编辑]
我应该提到我尝试了多种不同的 redirect_uri。 localhost:3000, 3003.
解决方案是从 SPA 应用进行令牌 API 调用。我能够成功请求。
我需要重新考虑如何组织和打电话。
我有一个带有 Rails BE 的 SPA React 应用程序。我正在使用 https://github.com/anthonyjgrove/react-google-login 生成带有 accessType 离线的 code
。这会成功生成一个代码,我可以取回它。
现在,我正在尝试使用此代码通过 POSTMAN
向 https://oauth2.googleapis.com/token 发出 POST 请求curl --location --request POST 'https://oauth2.googleapis.com/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'code=4/0AX4XfWhUc8IC12kr1115g71111' \
--data-urlencode 'redirect_uri=urn:ief:wg:oauth:2.0:oob' \
--data-urlencode 'client_id=x.apps.googleusercontent.com' \
--data-urlencode 'client_secret=secret' \
--data-urlencode 'scope=' \
--data-urlencode 'grant_type=authorization_code'
就使用 https://developers.google.com/oauthplayground 而言,这看起来是正确的。它在这里工作。
但是,当我尝试通过 curl 或 HttParty 发出请求时,它失败了。我不确定是否会发生这种情况,因为我从 SPA 应用程序获取代码,然后使用我的 BE 应用程序发出其他请求。
[编辑] 我应该提到我尝试了多种不同的 redirect_uri。 localhost:3000, 3003.
解决方案是从 SPA 应用进行令牌 API 调用。我能够成功请求。
我需要重新考虑如何组织和打电话。