如何在Java前端和后端使用Google Sign
在我的应用程序中,我只想得到用户的Gmail。我不使用任何谷歌服务。我想为Java前端添加Google登录,并在Java后端验证用户 我在看文件 通过在浏览器中打开以下链接,我可以获得代码 (获取)请求:如何在Java前端和后端使用Google Sign,java,google-signin,google-authentication,Java,Google Signin,Google Authentication,在我的应用程序中,我只想得到用户的Gmail。我不使用任何谷歌服务。我想为Java前端添加Google登录,并在Java后端验证用户 我在看文件 通过在浏览器中打开以下链接,我可以获得代码 (获取)请求: https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount? response_type=code& scope=openid& redirect_uri=http://localhost:3000/auth/
https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?
response_type=code&
scope=openid&
redirect_uri=http://localhost:3000/auth/&
client_id=113291176157....
{
code: '4/0AfDhmrjDTOo7zOrXxm98E...',
scope: 'openid',
authuser: '0',
prompt: 'none'
}
https://oauth2.googleapis.com/token
{
"code": "4/0AfDhmrjDTOo7zOrXxm98E...",
"client_id": "113291176157-aibhsqjf655ve...",
"client_secret": "lLjenLdeaJnd...",
"redirect_uri": "http://localhost:3000/auth",
"grant_type": "authorization_code"
}
{
"error": "redirect_uri_mismatch",
"error_description": "Bad Request"
}
/auth端点获取以下内容:
https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?
response_type=code&
scope=openid&
redirect_uri=http://localhost:3000/auth/&
client_id=113291176157....
{
code: '4/0AfDhmrjDTOo7zOrXxm98E...',
scope: 'openid',
authuser: '0',
prompt: 'none'
}
https://oauth2.googleapis.com/token
{
"code": "4/0AfDhmrjDTOo7zOrXxm98E...",
"client_id": "113291176157-aibhsqjf655ve...",
"client_secret": "lLjenLdeaJnd...",
"redirect_uri": "http://localhost:3000/auth",
"grant_type": "authorization_code"
}
{
"error": "redirect_uri_mismatch",
"error_description": "Bad Request"
}
现在要获取用户详细信息
(POST)请求:
https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?
response_type=code&
scope=openid&
redirect_uri=http://localhost:3000/auth/&
client_id=113291176157....
{
code: '4/0AfDhmrjDTOo7zOrXxm98E...',
scope: 'openid',
authuser: '0',
prompt: 'none'
}
https://oauth2.googleapis.com/token
{
"code": "4/0AfDhmrjDTOo7zOrXxm98E...",
"client_id": "113291176157-aibhsqjf655ve...",
"client_secret": "lLjenLdeaJnd...",
"redirect_uri": "http://localhost:3000/auth",
"grant_type": "authorization_code"
}
{
"error": "redirect_uri_mismatch",
"error_description": "Bad Request"
}
但我得到了以下回应:
https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?
response_type=code&
scope=openid&
redirect_uri=http://localhost:3000/auth/&
client_id=113291176157....
{
code: '4/0AfDhmrjDTOo7zOrXxm98E...',
scope: 'openid',
authuser: '0',
prompt: 'none'
}
https://oauth2.googleapis.com/token
{
"code": "4/0AfDhmrjDTOo7zOrXxm98E...",
"client_id": "113291176157-aibhsqjf655ve...",
"client_secret": "lLjenLdeaJnd...",
"redirect_uri": "http://localhost:3000/auth",
"grant_type": "authorization_code"
}
{
"error": "redirect_uri_mismatch",
"error_description": "Bad Request"
}
我已将重定向URL添加到凭据:
现在,我希望从Java前端打开一个浏览器窗口,让用户登录Google。我将接收code
值,它将获得id\u令牌
并验证用户
我见过一些javascript示例,它们直接获得id\u标记
所以我有很多问题
- 为什么我需要先获取
code
,然后再请求id\u令牌
- 为什么我得到
重定向\u uri\u不匹配
错误
授权码交换请求如下所示
URL:https://oauth2.googleapis.com/token
POST /token HTTP/1.1
Host: oauth2.googleapis.com
Content-Type: application/x-www-form-urlencoded
code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=your_client_id&
client_secret=your_client_secret&
redirect_uri=https%3A//oauth2.example.com/code&
grant_type=authorization_code
请求应该是POST
,数据应该作为urlencoded
数据发送,而不是在请求正文中
更多信息您是否碰巧看到了答案。我希望它对你有用。