如何执行身份验证以验证Google In App订阅

问题描述:

我想在服务器端验证来自androidpublisher API的用户订阅。我们有一个月的订阅我们的应用程序中的产品。我们正在关注的周期如下如何执行身份验证以验证Google In App订阅

  • 用户将获得产品列表。如果他已经订阅,他将获得完整的产品清单。否则,他将只获得免费产品。
  • 当用户订阅时,他将从Google购买该收据的收据。该收据将保存在我们的数据库中。无论何时用户要求产品列表,该收据都将通过Google API进行验证。如果它是有效的,他将得到全部结果。否则部分。

Google为此给出了以下API。

GET https://www.googleapis.com/androidpublisher/v2/applications/ {的packageName} /购买/订阅/ {subscriptionId} /令牌/ {令牌}

此API需要授权。我想下面的代码为这个

receipt_json = json.loads(subscription_object.receipt) 
service = build(serviceName='androidpublisher', version='v2', developerKey='key') 
response = service.purchases().subscriptions().get(
     packageName=receipt_json.get('packageName'), 
     subscriptionId=receipt_json.get('productId'), 
     token=receipt_json.get('purchaseToken')) 

但我得到如下回应

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "required", 
    "message": "Login Required", 
    "locationType": "header", 
    "location": "Authorization" 
    } 
    ], 
    "code": 401, 
    "message": "Login Required" 
} 
} 

请指导我应该怎么确认收货之前执行授权。一个重要的观点,我想验证服务器端的收据。不是客户端。因此,用户将不会看到或点击任何链接来授权此请求。

它似乎并不像你实际上发送任何认证密钥/令牌。您可以尝试使用OAuth2凭据(访问令牌或刷新令牌)进行身份验证:

import httplib2 
from googleapiclient.discovery import build 
from oauth2client.client import OAuth2Credentials 

credentials = OAuth2Credentials(...) 
http = credentials.authorize(http=httplib2.Http()) 

service = build(serviceName='androidpublisher', version='v2', http=http) 
+0

我有'client_secret','client_id'。 'OAuth2Credentials'至少需要'access_token','client_id','client_secret','refresh_token','token_expiry','token_uri'和'user_agent'。我怎样才能获得所有其他参数? –

+0

'access_token = None','token_expiry = datetime.datetime.now()','token_uri ='https:// accounts.google.com/o/oauth2/token'','user_agent'你可以设置任何你想要的,通常是你的应用名称加上你的应用版本,例如''Hassans App 1.0'(这将被用作请求的'User-Agent'头部。) –

+0

关于'refresh_token'也许[阅读此](https://developers.google.com/identity/protocols/ OAuth2用户)。如果您仍然有问题,请给我更多关于该链接中的哪些场景适用于您的用例的详细信息。 –