调用方法:purchase.subscriptions.get端点时出现UNAUTHENTICATED错误

问题描述

当我尝试从后端服务器致电https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{packageName}/purchases/subscriptions/{subscriptionId}/tokens/{token}验证我收到的购买信息时:

{
  "error": {
    "code": 401,"message": "Request is missing required authentication credential. Expected OAuth 2 access token,login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","errors": [
      {
        "message": "Login required.","domain": "global","reason": "required","location": "Authorization","locationType": "header"
      }
    ],"status": "UNAUTHENTICATED"
  }
}

如何从我的后端服务器进行身份验证(因此没有任何用户界面)?有什么方法可以在某处创建 API密钥并使用它进行身份验证吗?

解决方法

错误消息

请求缺少必需的身份验证凭据。预期的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据。参见https://developers.google.com/identity/sign-in/web/devconsole-project

声明需要授权。

如果您查看Method: purchases.subscriptions.get的文档

您会注意到,它指出此方法需要具有以下范围的授权

enter image description here

假设这是开发人员控制的帐户,并且您只想访问该帐户,则应考虑使用服务帐户进行授权。 Using a service account

此处的文档