谷歌/ OAuth的2 - 自动登录 [英] Google / OAuth 2 - Automatic logon

查看:225
本文介绍了谷歌/ OAuth的2 - 自动登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打了一下使用OAuth 2.0结合一些谷歌的API。虽然授权过程是很容易的,我最初的授权完成后面临的自动授权的问题。

所以:

  1。授权完成首次。 (用户授权访问,我得到的令牌等等等等)
2.用户退出应用程序
3.用户重新启动应用程序
4.如何登录自动在这里?
 

目前4点,我也有一个refresh_token所以我应该只要求使用request_token一个新的令牌。但我仍然不断收到关于我的电话401未授权的结果。

因此​​,我试图做的是,我的应用程序可以登录默默从而使用户不必每次授予访问权限。

解决方案

您应该可以使用下面的请求刷新的OAuth 2.0令牌:

  POST / O / oauth2 /令牌HTTP / 1.1
主持人:accounts.google.com
内容类型:应用程序/ x-WWW的形式urlen codeD

CLIENT_ID = 21302922996.apps.googleusercontent.com和放大器;
client_secret = XTHhXh1SlUNgvyWGwDk1EjXB和放大器;
refresh_token = 1 / 6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ&安培;
grant_type = refresh_token
 

正如在谷歌的OAuth 2.0文档

我只是尝试了一下使用curl,它按预期工作:

 卷曲-d CLIENT_ID = $ CLIENT_ID -d client_secret = $ CLIENT_SECRET -d refresh_token = $ REFRESH_TOKEN -d​​ grant_type = refresh_token https://accounts.google.com/o/oauth2/代币

{access_token:$ ACCESS_TOKEN,token_type:承载,expires_in:3600}
 

I'm playing a bit with OAuth 2.0 in combination with some Google API. Although the authorization process is quite easy, I'm facing a problem with the automatic authorization after the initial authorization has been completed.

So:

1. Authorization is done for the first time. (user grants access, I get the token etc etc)
2. User exits the application
3. User starts the application again
4. How to logon automatically here?

At point 4, I do have a refresh_token so I should just request a new token using that request_token. But I still keep getting 401 Unauthorized results on my calls.

So what I try to do is that my application can logon silently so that the user doesn't have to grant access every time.

解决方案

You should be able to refresh OAuth 2.0 token using the following request:

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

client_id=21302922996.apps.googleusercontent.com&
client_secret=XTHhXh1SlUNgvyWGwDk1EjXB&
refresh_token=1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ&
grant_type=refresh_token

As pointed in Google OAuth 2.0 documentation.

I just tried it out using curl and it works as expected:

curl -d client_id=$CLIENT_ID -d client_secret=$CLIENT_SECRET -d refresh_token=$REFRESH_TOKEN -d grant_type=refresh_token https://accounts.google.com/o/oauth2/token

{"access_token":"$ACCESS_TOKEN","token_type":"Bearer","expires_in":3600}

这篇关于谷歌/ OAuth的2 - 自动登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆