如何用Google APIs和Google的应用系统进行集成(4)----获得Access Token以通过一些Google APIs的OAuth2认证...

在上篇文章中:"如何用Google APIs和Google的应用系统进行集成(3)----调用发现Google APIs的RESTful的服务"一文中,我们直接用jdk的java.net.*中的包去调用"发现Google APIs的RESTful的服务"(Google Discovery RestFul web service),没有给服务器端传入任何的认证信息,我们能直接调用;但是对于另外的Google RESTFul的web服务,比如,Google 任务(Task) API,Google Calendar(日历)API,我们不能直接调用,必须提供相应的认证信息,即OAuth2的token信息,才能通过认证,才能调用其提供的服务。

关于OAuth2的具体技术知识,限于篇幅,我这边不在赘述,大家可以从网站找到很多这方面的资料,比如下面的网站,

(1)http://huoding.com/2011/11/08/126

(2) http://huoding.com/2010/10/10/8

在本篇文章中,主要给大家说的是---如何获得用户的OAuth2的Access Token(授权进入系统的令牌)去调用需要OAuth2认证的Google API.比如,本文中我们以获得Google 日历(Calendar) API的Access Token为例子。


(1)首先需要注册一个Google的账号。打开https://code.google.com/apis/console/这个网站,如果是第一次登陆,其会提示你注册一个账号。

按照要求注册一个Google账号。


(2) 注册后,登陆这个网站:https://code.google.com/apis/console/,并创建一个新的项目。


(3) 在左边的菜单中APIs&Auth-->APIs 选择Calendar API,并点击 “OFF”按钮(如果当前的Status(状态)为OFF(关)的话)使其变成“ON”状态。




(4)已在上面步骤(1)注册的账号,登陆下面的网站,https://developers.google.com/oauthplayground


(5) 在“Step1 Select&Authorize APIs”中,选择Calendar API V3中第一个。




(6) 点击 “Authorzie APIs”,此时会弹出一个网页,点击“Accept”按钮。此时Google APi的应用将会得到用户的授权。


(7) 在”Step2 Exchange authorization code for tokens“中,点击”Exchange Authorization code for tokens“按钮。



(8) 这个时候,在Access token 文本框中,将会显示,OAuth2的Access Token,注意当前的Access Token默认情况下是一个小时有效(3600秒)

过了3600秒后,这个Access Token将没有权限访问Google Calendar API。 如果下次需要继续延长当前的Access Token的使用,就需要用到Refresh

Token,关于Refresh Token的讲解,有时间且大家比较感兴趣的话,我将在后续章节给大家分享。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security可以通过使用Spring Security OAuth2库来集成OAuth2协议。具体地,你需要: 1. 添加Spring Security OAuth2依赖:在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.5.RELEASE</version> </dependency> ``` 2. 配置OAuth2客户端:在应用程序配置文件中添加以下内容,用于配置OAuth2客户端: ``` spring: security: oauth2: client: registration: google: client-id: <your-client-id> client-secret: <your-client-secret> scope: - email - profile provider: google: authorization-uri: https://accounts.google.com/o/oauth2/v2/auth token-uri: https://www.googleapis.com/oauth2/v4/token user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo user-name-attribute: sub ``` 3. 配置Spring Security:在应用程序配置文件中添加以下内容,用于配置Spring Security: ``` spring: security: oauth2: client: registration: google: client-id: <your-client-id> client-secret: <your-client-secret> scope: - email - profile provider: google: authorization-uri: https://accounts.google.com/o/oauth2/v2/auth token-uri: https://www.googleapis.com/oauth2/v4/token user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo user-name-attribute: sub ``` 4. 创建OAuth2客户端:在应用程序中创建一个OAuth2客户端,用于与认证服务器进行通信: ``` @Configuration @EnableOAuth2Client public class OAuth2ClientConfig { @Value("${spring.security.oauth2.client.registration.google.client-id}") private String clientId; @Value("${spring.security.oauth2.client.registration.google.client-secret}") private String clientSecret; @Bean public OAuth2ProtectedResourceDetails google() { ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails(); details.setAccessTokenUri("https://www.googleapis.com/oauth2/v4/token"); details.setClientId(clientId); details.setClientSecret(clientSecret); details.setGrantType("client_credentials"); details.setScope(Arrays.asList("email", "profile")); return details; } @Bean public OAuth2RestTemplate restTemplate(OAuth2ClientContext oauth2ClientContext) { return new OAuth2RestTemplate(google(), oauth2ClientContext); } } ``` 通过以上步骤,你就可以在Spring Security中集成OAuth2协议了。注意,以上提供的代码仅供参考,具体实现应该根据你的实际需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值