oauth - 谷歌 oauth2.0 405 错误

标签 oauth google-api google-oauth

我尝试使用以下链接使用 google oauth,但出现 405 错误,

你能告诉我参数是否正确吗?

client_id = changed to a diff value
response_type = code
scope= openid%20email
redirecturl = given the value based on what I registered in console.developers.com
login_hint = my gmail id..

https://accounts.google.com/o/oauth2/token?
 client_id=690178314820-85fvo4eq56se4mppdaf0pt6tnnjo552&
 response_type=code&
 scope=openid%20email&
 redirect_uri=http://test.webfactional.com&
 state=security_token%3D138r5719ru3e1%26url%3Dhttps://oa2cb.example.com/myHome&
 login_hint=myemail@gmail.com

我在浏览器中提出了上述获取请求..

最佳答案

有几个步骤可以让我更轻松地访问 Google,以便向您展示完整的流程。我的猜测是你被困在第二步,因为你没有将它作为帖子发送。

第一步:请求访问

https://accounts.google.com/o/oauth2/auth?client_id={clientid}.apps.googleusercontent.com&redirect_uri={From console}&scope=openid%20email&response_type=code

这只会显示要求他们批准您的窗口。一旦用户批准访问,您将获得一个一次性验证码。

第 2 步:交流 Authentication CodeAccessTokenRefreshToken .请注意,这需要作为 HTTP 发送发布 不是 HTTP Get。
https://accounts.google.com/o/oauth2/token
code={Authentication Code from step 1}&client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&redirect_uri=={From console}&grant_type=authorization_code

你应该得到一个 JSon 字符串,看起来像这样。
{
"access_token" : "ya29.1.AADtN_VSBMC2Ga2lhxsTKjVQ_ROco8VbD6h01aj4PcKHLm6qvHbNtn-_BIzXMw",
"token_type" : "Bearer",
"expires_in" : 3600,
"refresh_token" : "1/J-3zPA8XR1o_cXebV9sDKn_f5MTqaFhKFxH-3PUPiJ4"
}

现在你可以拿那个 Access_token并用它来提出您的要求。但是访问 token 的有效期只有 1 小时,然后它们会在那之前过期,您需要使用 Refresh_token获取新的访问 token 。此外,如果您想再次访问您的用户数据,您应该保存 refresh_token某个地方可以让您始终访问那里的数据。

第 3 步:使用刷新 token
https://accounts.google.com/o/oauth2/token
client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&refresh_token={RefreshToken from step 2}&grant_type=refresh_token

这次你只会取回访问 token ,因为你的刷新 token 是好的,直到用户删除身份验证或者你有 6 个月没有使用它。
{
"access_token" : "ya29.1.AADtN_XK16As2ZHlScqOxGtntIlevNcasMSPwGiE3pe5ANZfrmJTcsI3ZtAjv4sDrPDRnQ",
"token_type" : "Bearer",
"expires_in" : 3600
}

您可以在此处找到更多详细信息 Google 3 Legged oauth2 flow

关于oauth - 谷歌 oauth2.0 405 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24026431/

相关文章:

java - 我应该使用哪个 Java 库通过 Facebook、Gmail、本地身份验证等进行身份验证?

javascript - 使用 google+ api 对来自特定域的用户进行身份验证

java - 从 OAuth2.1 迁移到 OAuth2.0

c# - 权限不足 [403] 发送电子邮件

oauth-2.0 - 如何允许用户使用他们的 Google 帐户访问[读/写] 私有(private)融合表

google-api - 如何在Electron的WebView中将OAuth2 token 与gmail一起使用?

javascript - Google OAuth - 使用 "anonymous"作为 key 和 secret 是否不安全?

javascript - 无法使用服务帐户查询 Google Search Console API

java - 谷歌分析图表将应用程序安装到网络应用程序

android - 两个纬度和经度之间的行程距离