android - 在这个简单的 Android 应用程序案例中,我需要从 OAuth 2.0 获得什么?

标签 android oauth oauth-2.0 google-play subscription

故事:我正在制作一个 Android 应用程序,它允许用户购买订阅,并且不需要用户拥有帐户或登录。我想检查用户是否购买了订阅,the Google Play Android Developer API seems to provide this service .

问题 (TL;DR): 我应该将 OAuth 用作“网络应用程序”、“已安装的应用程序”、“服务应用程序”,还是不使用 OAuth以上?

问题:为了开始这个,我被告知:

Access to the Google Play Android Developer API is authenticated using the OAuth 2.0 Web Server flow. Before you can use the API, you will need to set up an APIs Console project, create a client ID and generate a refresh token. -source

很公平。然后有设置说明继续说:

On the second page, select web application and set the redirect URI and Javascript origins.

我的应用程序确实访问了互联网,但它是一个已安装的 Android 应用程序,不是网络应用程序,所以我没有“重定向 URI”或“Javascript 来源”来链接它.此外,这将需要用户登录,我不希望这样做,在我的情况下也没有必要(我只是想检查用户是否购买了订阅)。

因此,如果我尝试创建一个“安装的应用程序 (Android)”而不是“网络应用程序”,这仍然需要用户登录才能管理用户的资源。

我不想要这个。还有第三种替代方法,称为“服务帐户”,不需要用户登录:

A Service Account is used when you have a service that wants to handle its "own" resources (e.g., an App Engine app that manages Compute Engine resources), as opposed to the resources of an external user (e.g., the standard OAuth flow). Using a Service Account the app will be the owner of the resources... If you use a Service Account, you will only get data about the service's purchases. -source

我不确定这是否是我想要的...

最后还有这个选项:

The simplest flow is one where no end-user authorization is needed. You still need to identify your client application using the API key. -source

这看起来很完美!但是,最初有人告诉我,要使用 Google Play Android Developer API,我需要使用 OAuth 2.0 进行身份验证,而这不使用最初告诉我特别需要的客户端 ID。

最佳答案

您在这里尝试实现的目标至少存在 2 个问题:

  1. 当您在 Android 应用程序中处理服务器响应时,您的代码中会包含如下内容:

    if (isSubscriptionValid())
    

    有人可能会在他的设备上篡改您应用程序的 APK(这很容易),只需将检查替换为:

    if (true)
    

    攻击者无需订阅即可访问您的内容。

  2. 由于调用 API 必须由您的开发者帐户授权,并且在每个用户设备上亲自登录显然是不可取的,您必须使用服务帐户,因为您已经正确地理解了.

    然而,这些仅适用于 server-to-server interactions ,否则它会要求您将您的私钥存储在每个人的设备上,并且由于无法在 Android 设备上安全地存储数据,因此您不会满足此要求:

    The private key must be stored and managed securely.

Google recommends你有一个后端服务器来做这种检查。因此,您可以在将内容移交给客户端和其他事情之前决定订阅是否有效:

The API is designed to be used from your backend servers as a way of securely managing subscriptions, as well as extending and integrating subscriptions with other services.

如果您没有可用的后端服务器,则必须依赖 In-app Billing Notifications .

关于android - 在这个简单的 Android 应用程序案例中,我需要从 OAuth 2.0 获得什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12373914/

相关文章:

java - 如何在一个文件中拆分两个连续的 xml? (Java/安卓)

authentication - 通过代码使用 SSO 在多台机器上授权 WebAPI

ios - 如何验证 IDFV (idenitifierForVendor) 的供应商?

javascript - 如何使用 OAuth 存储和发送带有 API 请求的授权 token ?

java - 对齐 2 个文本,1 个正常,1 个相反

android - API 21 滑动标签 Activity

android - Google plus 在 android 上登录

zend-framework2 - 如何在 Apigility 中检查 oAuth2 范围?

android - 我的 CSS 在使用 javascript 时向左移动了一个 div .Toggle(包括屏幕截图)

javascript - 使用 JavaScript 的 Google OAuth2 刷新 token