c# - purchase.products.get 忽略 productId 值并在 ProductPurchase 中返回 null

标签 c# google-api google-play google-api-dotnet-client in-app

我正在向 google Android Publisher api 提出请求检查提供的应用内购买的 purchaseToken 是否正确。第一个问题是产品 ID 被忽略,我可以输入任何内容作为参数。只需要包名和purchaseToken是正确的。其次,为什么 ProductPurchase 中的 productId 和 purchaseToken 为空。我想检查提供的 purchaseToken 是否与 productId 匹配。当 productId 可以是任何东西并且没有返回响应时,我如何检查这对于特定的应用内购买 productId 是否有效?

    private static Task<ProductPurchase> GetInAppPurchase(string packageName, string productId, string purchaseToken)
    {
        GoogleCredential credential;
        using (Stream stream = new FileStream(@"api.json", FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            credential = GoogleCredential.FromStream(stream);
        }

        string[] scopes = {
            AndroidPublisherService.Scope.Androidpublisher
        };
        credential = credential.CreateScoped(scopes);

        BaseClientService.Initializer initializer = new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName       = "My Application"
        };
        
        
        
        AndroidPublisherService service = new AndroidPublisherService(initializer);

        return service.Purchases.Products.Get(packageName, productId, purchaseToken).ExecuteAsync();
    }

    private static void TestGooglePlayPurchaseApi()
    {
        string packageName = "validPackageName";
        string productId   = "validOrInvalidProductName";
        string purchaseToken = "validPurchaseToken";

        ProductPurchase purchase = GetInAppPurchase(packageName, productId, purchaseToken).Result;

        Console.WriteLine("Kind: " + purchase.Kind);
        Console.WriteLine("Quantity: " + purchase.Quantity);
        Console.WriteLine("AcknowledgementState: " + purchase.AcknowledgementState!);
        Console.WriteLine("ConsumptionState: " + purchase.ConsumptionState!);
        Console.WriteLine("DeveloperPayload: " + purchase.DeveloperPayload);
        Console.WriteLine("ETag: " + purchase.ETag);
        Console.WriteLine("OrderId: " + purchase.OrderId);
        Console.WriteLine("ProductId: " + purchase.ProductId);
        Console.WriteLine("PurchaseState: " + purchase.PurchaseState!);
        Console.WriteLine("PurchaseToken: " + purchase.PurchaseToken);
        Console.WriteLine("PurchaseType: " + purchase.PurchaseType!);
        Console.WriteLine("RegionCode: " + purchase.RegionCode);
        Console.WriteLine("PurchaseTimeMillis: " + purchase.PurchaseTimeMillis);
        Console.WriteLine("ObfuscatedExternalAccountId: " + purchase.ObfuscatedExternalAccountId);
        Console.WriteLine("ObfuscatedExternalProfileId: " + purchase.ObfuscatedExternalProfileId);
    }
这就是我正在接受的:
Kind: androidpublisher#productPurchase
Quantity:
AcknowledgementState: 1
ConsumptionState: 1
DeveloperPayload: {"developerPayload":"","is_free_trial":false,"has_introductory_price_trial":false,"is_updated":false,"accountId":""}
ETag:
OrderId: YYY.XXXX-XXXX-XXXX-XXXXX
ProductId:
PurchaseState: 0
PurchaseToken:
PurchaseType: 0
RegionCode: PL
PurchaseTimeMillis: 1601915754698
ObfuscatedExternalAccountId:
ObfuscatedExternalProfileId:
为什么 ProductId 为空?

最佳答案

如果您的数据库中只有一项,它将为空。很奇怪,但事实就是如此。
添加同一类别的第二个项目,您将开始获得正确的响应值。

关于c# - purchase.products.get 忽略 productId 值并在 ProductPurchase 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64217553/

相关文章:

c# - 如何将 wchar_t* 从 C++ 编码到 C# 作为输出参数或返回值?

c# - 在 WPF 中嵌入项目图标

android - 带有 GoogleSignIn 的 Google API - 错误 401 - 未经授权

c# - 从 Windows 桌面应用程序发送电子邮件错误报告

c# - 如何调用 method_getImplementation 的 IntPtr 结果

android - 使用新的 Flutter Apk 更新 Android 应用程序 (Google Play)

google-play - 我的应用程序不支持任何设备

google-play - 服务器何时返回 LICENSED_OLD_KEY?

php - Youtube Analytics API的速率限制?

google-api - 如何从我的应用程序中检查 Google Api 配额?