本地正常的Google IAP验证代码在Firebase Functions中报错
Google IAP收据验证:本地测试成功但部署到Firebase Functions后报错
我希望通过Firebase Functions验证Google IAP收据,但本地测试成功的相同代码部署到Functions后出现错误。
本地测试代码(test.ts,测试通过)
import { beforeEach, describe, expect, it } from "@jest/globals"; import { logger } from "firebase-functions/v1"; import { GoogleAuth } from "google-auth-library"; import { google } from "googleapis"; describe("test group", () => { it("test", async () => { const mockAsyncFunc = async () => { const auth = new GoogleAuth({ credentials: { client_email: "for-purchase-check@my_project.iam.gserviceaccount.com", private_key: "-----BEGIN PRIVATE KEY-----....-----END PRIVATE KEY----- ", project_id: "my_project", }, scopes: ["https://www.googleapis.com/auth/androidpublisher"], }); const androidPublisher = google.androidpublisher({ version: "v3", auth: auth, }); const response = await androidPublisher.purchases.products.get({ packageName: "com.my.domain", productId: "goods0", token: "....", }); logger.info("result:", response.data); console.log("data: " + response.data); console.log("autoRenewing: " + response.data.orderId); console.log("kind: " + response.data.kind); console.log("status: " + response.statusText); return { success: true, data: "data", }; }; const result = await mockAsyncFunc(); expect(result).toEqual({ success: true, data: expect.any(String), }); }); });
Firebase Functions代码(index.ts)
import { GoogleAuth } from "google-auth-library"; import { google } from "googleapis"; ... const auth = new GoogleAuth({ credentials: { client_email: "for-purchase-check@my_project.iam.gserviceaccount.com", private_key: "-----BEGIN PRIVATE KEY-----....-----END PRIVATE KEY----- ", project_id: "my_project", }, scopes: ["https://www.googleapis.com/auth/androidpublisher"], }); const androidPublisher = google.androidpublisher({ version: "v3", auth: auth, }); const response = await androidPublisher.purchases.products.get({ packageName: "com.my.domain", productId: "goods0", token: "...", }); ...
本地运行npm test结果
data: [object Object] autoRenewing: GPA.0000-0000-0000-00000 // 我将数字替换为0 kind: androidpublisher#productPurchase status: OK
Firebase Functions错误信息
Error: The purchase token is no longer valid. at Gaxios._request (/workspace/node_modules/gaxios/build/src/gaxios.js:142:23) at process.processTicksAndRejections (node:internal/process/task_queues:105:5) at async JWT.requestAsync (/workspace/node_modules/google-auth-library/build/src/auth/oauth2client.js:429:18) at async /workspace/lib/index.js:89:22 at async /workspace/lib/wrapper.js:27:24
相同代码为何出现这种差异?还需要提供其他信息吗?恳请帮助。
内容的提问来源于stack exchange,提问作者normidar
相关产品推荐
相关产品推荐

