如何用 Facebook 代码交换 access_token
我从客户端使用 Facebook 启动 oAuth2
,并在用户成功登录后获取代码。现在我想交换 access_token 的代码,如下所示:https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
如何用代码交换令牌?我需要使用 WebClient
执行 REST GET
请求,或者使用 Spring
控制器执行简单的重定向?我想在没有任何外部库的情况下做到这一点。
我认为我的流程应该如下所示:
- 用户点击
Login with Facebook
- 使用代码重定向到后端
- 获取代码并交换 access_token
- 调用 Graph QL用户数据
- 保存用户数据,生成JWT并返回前端。
I start oAuth2
with Facebook from client side and I get code after successful user login. Now I want exchange code for an access_token like here: https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
How can I exchange code for token? I need do REST GET
Request with WebClient
or do simple redirect with Spring
Controller? I want to do this without any external library.
My flow I think should look like:
- User clicks
Login with Facebook
- Redirected with code to backend
- Gets code and exchanges for a access_token
- Call Graph QL for user data
- Save user data, generate JWT and back to frontend.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从问题中我了解到您需要基本上实现 授权代码< /a> OAuth2.0协议流程。
收到代码后,您必须将其兑换为令牌。为此,您必须发送 POST 请求(通过 Spring 控制器)来访问 Facebook 的令牌端点。这样,您就必须发送您在创建 fakebook 客户端应用程序时收到的其他详细信息,例如 client_id、client_secret。
PS:我知道您在使用第三方库来实现此目的时可能会受到限制,但看看这个很棒的 open -source 库。
From the question I understand you need to basically implement Authorization code flow of the OAuth2.0 protocol.
After you receive code, you have to exchange it for a token. For this you would have to send a POST request(through your spring controller) to access token endpoint of Facebook. With this you would have to send additional details like client_id, client_secret which you have received when you created a fakebook client application.
PS:I understand you might have constraints with using third party libaries to achive this, but have a look at this awsome open-source library.