【发布时间】:2014-12-24 15:45:04
【问题描述】:
上下文——
- 我正在构建一个使用 Google Cal 和 Google+ API 的 Web 应用程序。
- 我需要获取刷新令牌,因为一旦用户通过站点/应用程序进行身份验证,一些调用会在他们登录后发生在幕后(其中许多会在 1 小时后发生,其中初始access_token 的有效期)
据我了解,这是我必须遵循的流程:
- 通过 Google 控制台注册 Web 应用程序 API - 完成。
- 提示用户使用我的应用程序进行身份验证,通过使用以下配置变量的调用完成:
var config = {
'client_id': MY_CLIENT_ID',
'scope': 'https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/userinfo.email',
'response_type': 'code',
'access_type': 'offline'
};
- 然后,使用通过上述 auth() 调用返回的 Google 对象,再次调用以获取 access_token 和 refresh_token。
https://developers.google.com/accounts/docs/OAuth2WebServer#refresh
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=CODE_RETURNED
client_id=CLIENT_ID_RETURNED
client_secret=API_CLIENT_SECRET
redirect_uri=API_REDIRECT_API
grant_type=authorization_code
然而,当我尝试运行此调用时,我总是会遇到某种类型的错误。现在我遇到以下问题:
{
error: "redirect_uri_mismatch"
}
我在 Google API 设置页面和代码中都将以下内容列为我的重定向 uri:
http://localhost/
以前使用过此流程的人有什么建议吗? 我需要设置不同的东西来获取刷新令牌吗?
【问题讨论】:
-
您是否将
redirect_uri设置为与 Google Developers 设置中相同的值?此外,我认为您还需要redirect_uri进行第一次通话。 -
重定向URI需要映射到一个页面例如:localhost/google-api-php-client-samples/oauth2.php
-
@DaImTo 该页面必须存在吗?如果是这样,该页面的作用是什么?
-
Google 说:您在 Developers Console 中设置的重定向 URI 决定了 Google 将响应发送到您的身份验证请求的位置。所以我假设对于本地测试,这将是localhost/project/page.php(我正在拨打电话并希望根据响应执行逻辑)
标签: javascript google-api access-token