Google Auth (OAuth 2.0) for Titanium

[url=https://bitbucket.org/miroslavmagda/google-auth-for-titanium]Google OAuth 2.0 for Titanium[/url] is a module for [url=http://www.appcelerator.com/platform]Appcelerator Titanium[/url] that i’ve created for use in my mobile apps. It allows to authenticate user with Google OAuth 2.0 and then work with Google Apis.

To understand how it works you must check [url=https://developers.google.com/accounts/docs/OAuth2InstalledApp]Google Accounts Authentication and Authorization docs[/url].

[b]Example usage:[/b]
Check the demo App to see it in action. It is pretty simple but you will need to get your CLIENT_ID and CLIENT_SECRET from [url=https://code.google.com/apis/console/]Google Api Console[/url].

[b]Titanium code:[/b]

//initialize module
var GoogleAuth = require('modules/googleAuth');
var googleAuth = new GoogleAuth({
clientId : 'CLIENT_ID',
clientSecret : 'CLIENT_SECRET',
propertyName : 'googleToken',
scope : ['https://www.googleapis.com/auth/tasks', 'https://www.googleapis.com/auth/tasks.readonly']
});
//create some button
var sync = Ti.UI.createButton({
title : 'Sync'
});
//do some action...
sync.addEventListener('click', function() {
googleAuth.isAuthorized(function() {
Ti.API.info('Access Token: ' + googleAuth.getAccessToken());
//user is authorized so do something... just dont forget to add accessToken to your requests

}, function() {
//authorize first
googleAuth.authorize();
});
});


[b]Titanium demo:[/b]
[img]http://dl.iteye.com/upload/attachment/0081/8617/8ba46b46-dbfe-3519-a853-96e86a0fb3d4.png[/img]

[b]Titanium module:[/b]
[url=http://dl.iteye.com/topics/download/af8f9bd8-0a49-34f6-8863-73cf650a8b13]Download google-auth-for-titanium[/url]

[url=https://bitbucket.org/miroslavmagda/google-auth-for-titanium]https://bitbucket.org/miroslavmagda/google-auth-for-titanium[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OAuth2.0是一种授权框架,用于保护API、Web应用程序和移动应用程序的资源。OAuth2.0定义了四种授权方式:授权码、隐式、密码和客户端凭证。在Java中,我们可以使用Spring Security框架来实现OAuth2.0服务端和客户端的实现。 OAuth2.0服务端实现: 1. 引入Spring Security OAuth2.0依赖 ``` <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.4.RELEASE</version> </dependency> ``` 2. 配置OAuth2.0认证服务器 在Spring Security配置类中增加如下配置: ``` @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private DataSource dataSource; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.jdbc(dataSource); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); } } ``` 其中,@EnableAuthorizationServer注解表示启用OAuth2.0认证服务器,configure(ClientDetailsServiceConfigurer clients)方法用于配置客户端的信息,configure(AuthorizationServerEndpointsConfigurer endpoints)方法用于配置认证服务器的端点。 3. 配置Spring Security 在Spring Security配置类中增加如下配置: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } } ``` 其中,@EnableWebSecurity注解表示启用Spring Security,configure(AuthenticationManagerBuilder auth)方法用于配置用户信息,authenticationManagerBean()方法用于获取认证管理器。 OAuth2.0客户端实现: 1. 引入Spring Security OAuth2.0依赖 ``` <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.4.RELEASE</version> </dependency> ``` 2. 配置OAuth2.0客户端 在Spring配置文件中增加如下配置: ``` security.oauth2.client.client-id=<client-id> security.oauth2.client.client-secret=<client-secret> security.oauth2.client.access-token-uri=<access-token-uri> security.oauth2.client.user-authorization-uri=<user-authorization-uri> security.oauth2.client.token-name=<token-name> security.oauth2.client.authentication-scheme=<authentication-scheme> security.oauth2.client.client-authentication-scheme=<client-authentication-scheme> ``` 其中,security.oauth2.client.client-id表示客户端ID,security.oauth2.client.client-secret表示客户端秘钥,security.oauth2.client.access-token-uri表示访问令牌的URI,security.oauth2.client.user-authorization-uri表示用户授权的URI,security.oauth2.client.token-name表示令牌的名称,security.oauth2.client.authentication-scheme表示认证方案,security.oauth2.client.client-authentication-scheme表示客户端认证方案。 3. 使用RestTemplate访问受保护的资源 ``` RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "Bearer " + accessToken); HttpEntity<String> entity = new HttpEntity<>(headers); ResponseEntity<String> response = restTemplate.exchange( "http://localhost:8080/api/protected", HttpMethod.GET, entity, String.class); String body = response.getBody(); ``` 其中,accessToken为获取到的访问令牌,"http://localhost:8080/api/protected"为受保护的资源的URI。最后,我们可以通过RestTemplate访问受保护的资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值