简体   繁体   English

在Android上使用OAuth 2.0使用Google API的最佳方法

[英]Best way to use Google APIs using OAuth 2.0 on Android

I'm trying to migrate an Android application using OAuth 1.0a to OAuth 2.0. 我正在尝试将使用OAuth 1.0a的Android应用程序迁移到OAuth 2.0。 (using the Google API Client Library for Java/Android for my OAuth 2.0 needs). (我的OAuth 2.0需要使用Java / Android版Google API客户端库)。

What is the best/preferred solution for accessing Google APIs using OAuth 2.0 on an Android platform that takes into account the usability aspect as well. 考虑到可用性方面,在Android平台上使用OAuth 2.0访问Google API的最佳/首选解决方案是什么。 The user should be able to autorize access in an easy way, seamlessly integrating with my Android app. 用户应该能够以一种简单的方式自动授予访问权限,并与我的Android应用程序无缝集成。

The application is currently using the OAuth 1.0 web based flow, where my application pops a browser to let the user authorize access, and using a custom redirect URI, my application is capable of retrieving an access token. 该应用程序当前使用基于OAuth 1.0 Web的流程,其中我的应用程序弹出一个浏览器以允许用户授权访问,并且使用自定义重定向URI,我的应用程序能够检索访问令牌。 It works pretty well, but I didn't like the fact that I need to leave my app in order to pop a brower to display a webpage. 它工作得很好,但是我不喜欢我需要离开我的应用程序才能弹出浏览器来显示网页的事实。 I was thinking that OAuth 2.0 might work around this, and allow for a better user experience. 我以为OAuth 2.0可以解决此问题,并提供更好的用户体验。

I started looking at the Adroid AccountManager-OAuth2 integration as outlined at Google IO , as it doesn't involve a webbrowser, and is more tightly coupled with Android, but it is simply not working the way it should. 我开始研究Google IO概述Adroid AccountManager-OAuth2集成 ,因为它不涉及Web浏览器,并且与Android的联系更加紧密,但是根本无法正常工作。 It's not documented, and unclear if it will remain a viable option for the future. 它没有记录,也不清楚将来是否仍然是可行的选择。

I've now started investigating the standard OAuth 2.0 web flow. 我现在开始研究标准的OAuth 2.0网络流程。

Here I seem to be having 2 options : 在这里,我似乎有2个选择:

Configure the OAuth 2.0 client as an installed app, and use the urn:ietf:wg:oauth:2.0:oob redirect URI. 将OAuth 2.0客户端配置为已安装的应用,然后使用urn:ietf:wg:oauth:2.0:oob重定向URI。

Not very clean solution, as I'm not going to have my users copy-paste some code into my app. 解决方案不是很干净,因为我不会让用户将一些代码复制粘贴到我的应用程序中。 This is not user-friendly at all. 这根本不是用户友好的。

The Using OAuth 2.0 to Access Google APIs docs mention that there is some way of polling the title of the page to parse out the URL, but I also see a lot of usability issues with that, and don't really feel like writing this kind of plumbing code. 使用OAuth 2.0访问Google API文档提到了一种通过轮询页面标题来解析URL的方法,但与此同时,我也看到了很多可用性问题,并且真的不喜欢编写这种形式水暖代码。 If a client library exists that would do that for me, I'd be happy to investigate this further, but for now, I've dropped this option. 如果存在可以为我完成任务的客户端库,我很乐意对此做进一步调查,但是现在,我已经放弃了该选项。

Configure the OAuth 2.0 client as a webapp, and use a redirect URI. 将OAuth 2.0客户端配置为网络应用,并使用重定向URI。

Here I noticed non-standard schemes are prohibited in OAuth 2.0. 在这里,我注意到OAuth 2.0中禁止使用非标准方案。 Before, it was possible to use something like xoauth://callback, but that's not allowed anymore. 以前,可以使用xoauth:// callback之类的东西,但是现在不再允许了。 When configuring a redirect URI like http://mysite.com/oauth2/callback , I'm unable to have Android open up my activity when the Google OAuth 2.0 page redirects, despite having setup a proper intent filter for it. 在配置重定向URI(例如http://mysite.com/oauth2/callback)时,即使设置了适当的意图过滤器,我也无法在Android OAuth 2.0页面重定向时打开Android的活动。 The http://mysite.com/oauth2/callback is simply displayed in my browser. http://mysite.com/oauth2/回调仅显示在我的浏览器中。

The following does work 以下工作

Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("http://mysite.com/oauth2/callback"));
startActivity(i);

But when the Google OAuth 2 page redirects to that same URL, it is simply displayed in the browser. 但是,当Google OAuth 2页面重定向到相同的URL时,它仅显示在浏览器中。

Even if this would work, the user would still be presented with a chooser popup (open in browser or open using my Android Activity). 即使这可行,仍会向用户显示选择器弹出窗口(在浏览器中打开或使用“我的Android活动”打开)。 From a usability perspective, this is also not acceptable. 从可用性的角度来看,这也是不可接受的。

I'm looking for a better solution than the ones outlined here. 我正在寻找比这里概述的解决方案更好的解决方案。

Regards, Davy 戴维

I ended up using a WebView component to load up the Google Authorization URL. 我最终使用了WebView组件来加载Google授权URL。 Using a WebviewClient, I was able to intercept the pages being loaded into the Webview, and as such, when the user accepts or denies the authorization request, I was able to continue the flow. 使用WebviewClient,我可以拦截正在加载到Webview中的页面,因此,当用户接受或拒绝授权请求时,我可以继续执行该流程。

If the user accepts, the URL that Google redirects to contains a "code" request param, and the application is able to exchange it for an OAuth 2.0 token. 如果用户接受,则Google重定向到的URL包含“代码”请求参数,并且应用程序可以将其交换为OAuth 2.0令牌。 If the user does not accept, the URL that Google redirects to contains a "error" request param, and the application can handle the non-happy scenario. 如果用户不接受,则Google重定向到的URL包含“错误”请求参数,应用程序可以处理不满意的情况。

I've written down everything in a blog post : Oauth 2.0 flow in Android 我已经在博客文章中写下了所有内容: Android中的Oauth 2.0流

The post also contains a sample Android app using the OAuth 2.0 flow with the Latitude API. 该帖子还包含一个使用OAuth 2.0流和Latitude API的示例Android应用。 Sample code is available in GitGub . GitGub中提供了示例代码

Play Services were introduced at Google I/O 2013 and are now the official way to use OAuth2 in Android . Play服务是在Google I / O 2013上引入的,现已成为在Android中使用OAuth2的官方方式 They do not require a WebView. 他们不需要WebView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM