接入google 登录 google oauth2

注册谷歌账号 去创建 一个app设置的 url 访问 一定要和第二步中的http://www.newplat.com/google/googleLogin 一致

demo : https://developers.google.com/oauthplayground
文档 :https://developers.google.com/accounts/docs/OAuth2Login
修改应用:https://cloud.google.com/console/project/xxxxxxxxxx/apiui/credential

scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email


1. https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/userinfo.email&response_type=code&redirect_uri=http://www.newplat.com/google/googleLogin&client_id=xxxxxxxxxxx.apps.googleusercontent.com

2.http://www.newplat.com/google/googleLogin?code=4/eYbEPfgh5yY3aTGtWPuaYQrtO_GG.0sy18st9VmcfXE-sT2ZLcbSvGvSHhwI

3.post https://accounts.google.com/o/oauth2/token
code=4%2F6VO4EljtH_rFmbhcJZdZXFc572Lu.ImOFJCmmwDMXXE-sT2ZLcbQVlaCahwI&
redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&
client_id=xxxxxxxxxxx.apps.googleusercontent.com
&scope=&
client_secret=************&
grant_type=authorization_code

返回:
access_token     A token that can be sent to a Google API.
id_token     A JWT that contains identity information about the user that is digitally signed by Google.
expires_in     The remaining lifetime of the access token.
token_type     Identifies the type of token returned. At this time, this field always has the value Bearer.
refresh_token (optional)     This field is only present if access_type=offline is included in the authentication request. For details, see Refresh tokens.

4.https://www.googleapis.com/userinfo/v2/me  主要使用 token_type+空格+access_token
设置http 的header

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestProperty("Authorization", authorization);

格式:

Authorization: Bearer ya29.1.AADtN_Xp8k1tQyAjXIqwZjPehZgEi2NJpIIItcrtyHdFrNLqsDvtX8KgOA4GJ0w

猜你喜欢

转载自hyl198611.iteye.com/blog/2009720