Python - 使用 Google API 从 GAE 获取用户购买



我正在尝试使用来自服务器端的Google api调用(Google App Engine)获取用户的订阅。我浏览了我能找到的所有文档,但我无法清楚地理解应该如何正确完成此操作。到目前为止,我已经尝试了以下方法:

    credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/androidpublisher')
    http = credentials.authorize(httplib2.Http(memcache))
    service = build('androidpublisher', 'v2', developerKey='[DEVELOPER_KEY]', http=http)
    request = service.purchases().subscriptions().get(packageName='com.example', subscriptionId=self.orderId, token=self.token)
    response = request.execute(http)
    print json.dumps(response, sort_keys=True, indent=4)

但是我得到以下回应:

http错误:https://www.googleapis.com/androidpublisher/v2/applications/com.ichoose.bettercalls/purchases/subscriptions/incoming_and_outgoing_single_line_monthly/tokens/dfsdfdsfds?alt=json&key=AIzaSyDdxzjIEZme76VEVMKGaE7R4qTdsQrNloA 返回"无效凭据>

开发者密钥取自 google 开发者控制台 --> API 和身份验证 --> 凭据 --> 服务器应用程序的密钥 --> API KEY。这是对的吗?

如果有人能清楚地解释(最好是代码示例)应该如何做到这一点,我将不胜感激。多谢。

首先,您必须确保已将Google Play开发者控制台链接到API项目,然后可以设置API Access Client。请阅读此内容 [1]。

您需要创建/使用服务帐户(在Google开发人员控制台上生成),更多信息请点击此处[2]。

OAuth 身份验证的典型代码如下:

SERVICE_ACCOUNT_EMAIL = (
    'ENTER_YOUR_SERVICE_ACCOUNT_EMAIL_HERE@developer.gserviceaccount.com')
def main():
  # Load the key in PKCS 12 format that you downloaded from the Google APIs
  # Console when you created your Service account.
  f = file('key.p12', 'rb')
  key = f.read()
  f.close()
  # Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with the Credentials. Note that the first parameter, service_account_name,
  # is the Email address created for the Service account. It must be the email
  # address associated with the key that was created.
  credentials = client.SignedJwtAssertionCredentials(
      SERVICE_ACCOUNT_EMAIL,
      key,
      scope='https://www.googleapis.com/auth/androidpublisher')
  http = httplib2.Http()
  http = credentials.authorize(http)
  service = build('androidpublisher', 'v2', http=http)

其中SERVICE_ACCOUNT_EMAIL是与服务帐户关联的电子邮件地址,key.p12 是包含为该服务帐户生成的密钥的文件。

您可以在 Google Developers Console 上获取 -> API 和身份验证 ->凭据--->服务帐户。

[1] https://developers.google.com/android-publisher/getting_started

[2] https://developers.google.com/api-client-library/python/auth/service-accounts

相关内容

  • 没有找到相关文章

最新更新