Google OAuth2 for Android(type of web OAuth)
Google OAuth2 for Android(type of web OAuth)
一、应用创建和注册
1.登录google
登录Google 应用
2.创建项目
3.创建OAuth同意屏幕
4.创建OAuth客户端
5.查看Google client id 和sercret key
二、Android app中如何使用OAuth客户端
1.配置和提交授权申请
使用Github第三方的libAndroidOAuth
请求Google授权代码如下:
public void googleLogin(View v) {GoogleOAuth.login(this).setClientId(Credentials.GOOGLE_CLIENT_ID).setClientSecret(Credentials.GOOGLE_CLIENT_SECRET).setAdditionalScopes("https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/user.birthday.read https://www.googleapis.com/auth/youtube").setRedirectUri(Credentials.GOOGLE_REDIRECT_URI).setCallback(new OnLoginCallback() {@Overridepublic void onSuccess(String token, SocialUser user) {afterLogin(token, user);}@Overridepublic void onError(Exception error) {error.printStackTrace();}}).init();}
- GOOGLE_CLIENT_ID:前文创建应用生成的client id;
- GOOGLE_CLIENT_SECRET:前文创建应用生成的secret key;
- Scopes:https://www.googleapis.com/auth/plus.login为认证服务器地址(必填);后面两个网址为申请的授权权限(可根据需求自定义):如读取生日和使用youtube api相关权限;
-RedirectUri:完成授权后的重定向uri
上述方式可以获取access token,但是可能获取不到refresh token,这时候需要重写init();
将final String authUrl = service.getAuthorizationUrl();
修改为final String authUrl = service.getAuthorizationUrl()+"&access_type=offline";
2.获取授权成功返回的accss token和refresh token
// Generating a complex state for better security// http://twobotechnologies.com/blog/2014/02/importance-of-state-in-oauth2.htmlpublic void init() {final String state = UUID.randomUUID().toString();final OAuth20Service service = new ServiceBuilder().apiKey( clientId ).apiSecret( clientSecret ).callback( redirectUri ).state( state ).scope( scopes ).build( oAuth.api );final String authUrl = service.getAuthorizationUrl() + "&access_type=offline";Log.d( "1111", "-----authUrl=" + authUrl );ConsentDialog.newInstance( authUrl, state ).setOnGetCodeCallback( new OnGetCodeCallback() {@Overridepublic void onSuccess(final String code) {AsyncTask.execute( new Runnable() {@Overridepublic void run() {try {final OAuth2AccessToken accessToken = service.getAccessToken( code );final SocialUser account = oAuth.getAccount( service, accessToken );oAuth.activity.runOnUiThread( new Runnable() {@Overridepublic void run() {Log.d( "1111", "accessToken.getRefreshToken() =" + accessToken.getRefreshToken() );callback.onSuccess( accessToken.getAccessToken(), account );Log.d( "1111", "accessToken.getRefreshToken() =" + accessToken.getRefreshToken() );}} );} catch (final Exception e) {oAuth.activity.runOnUiThread( new Runnable() {@Overridepublic void run() {callback.onError( e );}} );}}} );}@Overridepublic void onError(Exception error) {callback.onError( error );}} ).show( oAuth.activity.getFragmentManager(), ConsentDialog.class.getName() );}}
注意回调onSuccess是成功,onError是失败。通过accessToken 可以获取accessToken.getAccessToken()和accessToken.getRefreshToken()。
至此,Google OAuth 认证完成。
三、参考文献
https://developers.google.com/identity/protocols/OAuth2
Google OAuth2 for Android(type of web OAuth)相关推荐
- Android xml资源文件中@、@android:type、@*、?、@+含义和区别
一.@代表引用资源 1.引用自定义资源.格式:@[package:]type/name android:text="@string/hello" 2.引用系统资源.格式:@andr ...
- android webserver mysql,Android手机变身Web服务器,BitWebServer简单评测
昨晚逛Play商店发现居然有这软件,支持lighttpd.php.mysql,可以让Android手机变身Web服务器,觉得很有意思,果断下了个玩玩~~ 随便放了一个静态页面,想看效果的可以戳 222 ...
- Internal error. Please report to https://code.google.com/p/android/issues
Android Studio启动后又弹出了一个错误: Internal error. Please report to https://code.google.com/p/android/issues ...
- google map for Android
因为政策原因,google map在国内鲜有人使用,但是如果涉及到国外的项目,又不得不和google map接触,借着自己做的项目,在这里简单介绍一下google map for Android的使用 ...
- AndServer,一个Android端的web服务器
版权声明:转载必须注明本文转自严振杰的博客:http://blog.yanzhenjie.com 大家好,今天跟大家介绍一个让原生Android也可以做Web开发的开源项目--AndServer. 开 ...
- Android Studio重构之路,我们重新来了解一下Google官方的Android开发工具
Android Studio重构之路,我们重新来了解一下Google官方的Android开发工具 记得我的第一篇博客就是写Android Studio,但是现在看来还是有些粗糙了,所有重构了一下思路, ...
- Google Developers 认证团队推出 Mobile Web Specialist 认证
如果您是一名网络开发者,就一定知道这是一个鱼龙混杂的市场,而您也想让自己从其他网络开发者中脱颖而出.想要展示您具备构建自适应和灵活网络应用的技能吗? Google Developers 认证团队荣幸地 ...
- google app engine for java 的web应用程序
在IT业步入"云"时代的时候,Google App Engine无疑是引领"云"技术潮流的一支生力军.但在App Engine诞生后的很长一段时间里,它都只支持 ...
- Google+ 团队的 Android UI 测试
https://github.com/bboyfeiyu/android-tech-frontier/tree/master/android-blog/Google%2B%20%E5%9B%A2%E9 ...
最新文章
- python实现平衡二叉树_LeetCode 110. 平衡二叉树 | Python
- 2020 诺贝尔奖「第一棒」:英美三位科学家摘得桂冠!
- LNMP - Nginx访问控制
- 故事工程学:人工智能和程序化叙事生成
- 如何解决Ubuntu 12.04(64位)系统在virtualbox环境下无法开机自动挂载共享目录的问题
- 有关Canvas的一点小事—图像绘制
- 为什么现在辞职的年轻教师越来越多?
- ubuntu下如何查看和设置分辨率 (转载)
- 用友发布智多星 AI 企业大脑,其核心组成原来是这样的
- Web前端开发初学者十问集锦(4)
- Vue项目按需打包Lodash
- 法兰克机械手手动操作_学习FANUC机器人编程设定,必懂这2个技巧!
- Computer Science | Retrosynthesis:关于逆合成路线规划
- json和jsonb类型——PostgreSQL
- Amit Chadha出任LT Technology Services 首席执行官兼董事总经理
- 安装时间大于30秒_30个住宅室内消防设置检验要点!硬核干货!
- 《第一行代码》学习笔记——第1章 开始启程,你的第一行Android代码
- 31个有点意思数据可视化作品!
- 线段树的建树 单点修改 区间查询
- java三个技术平台_java三大技术平台是什么