【发布时间】:2020-08-19 17:57:18
【问题描述】:
我正在尝试将 Google 日历应用到我们的网络项目中。在搜索并尝试了我包中的几乎所有技巧之后,在用户同意访问日历后,我无法获得刷新令牌。我想在我的前端(访问令牌和刷新令牌)获取用户的日历访问权限,并为其提供后端(python)以便稍后刷新访问令牌。 在尝试了几种可用的解决方案之后,我正在使用这个 - 在 html -
<script src="https://apis.google.com/js/client.js"></script>
在 javascript 中点击按钮 -
gapi.auth.authorize({
client_id: xxx.apps.googleusercontent.com,
client_secret: 'XYZ',
scope: "https://www.googleapis.com/auth/calendar",
immediate: false,
response_type: 'code token',
access_type:'offline',
prompt:'consent'
}, handleAuthResult);
由此我能够得到以下响应 -
{
"token_type": "Bearer",
"access_token": "XXX",
"scope": "email profile openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/calendar",
"login_hint": "XXX",
"expires_in": "3559",
"id_token": "XXX",
"session_state": null,
"expires_at": "1597689877",
"code": "4/3XXXXX",
"status": {
"signed_in": true,
"method": "PROMPT",
"google_logged_in": true
},
"g-oauth-window": null,
"client_id": "XXX.apps.googleusercontent.com",
"cookie_policy": "single_host_origin",
"response_type": "code token",
"authuser": "2",
"issued_at": "1597686278"
}
但仍然没有刷新令牌的迹象,我尝试通过此 api 交换访问令牌和 ID 令牌的代码 -
let request_params = JSON.stringify({
code: '4/3XXXXX',
client_id: 'XXX.apps.googleusercontent.com',
client_secret: 'XXX',
redirect_uri: 'http://localhost:5000',
grant_type: 'authorization_code'
});
$.ajax({
url: 'https://oauth2.googleapis.com/token',
type: 'POST',
data: request_params,
headers: {
'Content-type': 'application/x-www-form-urlencoded'
},
contentType: "application/x-www-form-urlencoded",
success: (res) => {
console.log(res, 'tokens')
},
error: (error) => {
console.log('An error occurred', error)
}
});
但这也给出了一个无效的 grant_type 错误。 任何帮助都非常受欢迎。 另外请让我知道是否有任何替代方法可以让我使用我的服务器直接重定向以获取身份验证而不是 javascript。
【问题讨论】:
-
客户端授权不返回刷新令牌,这将是一场安全噩梦。使用服务器端语言。
-
是的,这就是我之前的想法,并尝试通过将 python sdk 用于谷歌日历来做到这一点,但是通过运行本地服务器获得凭据,我避免这样做,因为它只会有帮助我在本地主机上。关于如何在不运行本地服务器的情况下执行此操作的任何建议?
-
如果您不想运行服务器,则在访问令牌过期时请求用户访问将是您唯一的选择。如果您没有服务器来托管它,您将如何托管它?
标签: javascript ajax oauth-2.0 google-calendar-api refresh-token