Google Play 开发者 API 返回 401 错误:请求缺少所需的身份验证凭据

问题描述 投票:0回答:1

我有一个云功能(环境:gen2)用于验证Google Play订阅购买。

我使用两个服务帐户:

  • 账户1用于构建和部署功能、权限:
    • 工件注册表创建推送写入器
    • 云构建服务帐号
    • 编辑器
    • 日志编写器
    • 存储对象管理。
  • 帐户 2 用于与 Google Play Developer API 交互,权限:
    • 云函数调用器
    • 云运行调用者
    • 编辑器

当我从 android studio 调用该函数时,出现此错误:

com.google.firebase.functions.FirebaseFunctionsException:在 okhttp3.RealCall$AsyncCall.execute(RealCall.java:203) 处的 com.google.firebase.functions.FirebaseFunctions$2.onResponse(FirebaseFunctions.java:388) 处未经身份验证。内部.NamedRunnable.run(NamedRunnable.java:32)

在 Cloud Shell 中测试时,出现此错误:

{
    "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",
        "details": [
            {
                "@type": "type.googleapis.com/google.rpc.ErrorInfo",
                "reason": "CREDENTIALS_MISSING",
                "domain": "googleapis.com",
                "metadata": {
                    "method": "androidpublisher.SubscriptionPurchasesService.Get",
                    "service": "androidpublisher.googleapis.com"
                }
            }
        ]
    }
}

Python代码:

import os
import json
from google.oauth2 import service_account
from googleapiclient.discovery import build
from flask import Flask, request, jsonify
from google.auth import default

app = Flask(__name__)

@app.route('/verify_purchase', methods=['POST'])
def verify_purchase():
    try:
        # Use Application Default Credentials (ADC)
        credentials, project = default(scopes=['https://www.googleapis.com/auth/androidpublisher'])
        androidpublisher = build('androidpublisher', 'v3', credentials=credentials)
    except Exception as e:
        return jsonify({"error": "Failed to initialize credentials or service"}), 500

    data = request.get_json()
    purchase_token = data.get('token')
    package_name = "workout_wrecker"
    subscription_id = "premium"

    if not purchase_token:
        return jsonify({"error": "Token is required"}), 400

    try:
        subscription = androidpublisher.purchases().subscriptions().get(
            packageName=package_name,
            subscriptionId=subscription_id,
            token=purchase_token
        ).execute()

        return jsonify(subscription), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500

# This is the entry point for Google Cloud Function
def hello_http(request):
    return verify_purchase()

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))



我尝试了两个服务帐户的不同权限,但无济于事。我还启用了 google play android 开发者 API。如果需要更多信息,请评论,我会更新。我之前没有使用过云功能,请耐心等待,谢谢!

python google-cloud-functions
1个回答
0
投票

好吧,所以我盲目地复制并粘贴了谷歌云代码而不更改 代码的“$(gcloud auth print-identity-token)”部分。

© www.soinside.com 2019 - 2024. All rights reserved.