【问题标题】:Access Google Play Android Developer API from .NET Web API从 .NET Web API 访问 Google Play Android Developer API
【发布时间】:2014-05-08 12:05:08
【问题描述】:

这是我正在使用的代码,为本示例进行了简化:

Public PackageName As String = "com.yourdomain.app"
Public InAppPurchaseProductID As String = "com.yourdomain.app.subscriptionXYZ"
Public PublicLicenseKey As String = "YOUR_LONG_ENCODED_LICENSE_KEY"
Public ConsolePrivateKeyCertificateData As Byte() 'method for reading p12 file contents left out for simplicity
Public ConsoleCertificate As X509Certificate2 = New X509Certificate2(ConsolePrivateKeyCertificateData, "notasecret", X509KeyStorageFlags.MachineKeySet Or X509KeyStorageFlags.Exportable)
Public ConsoleServiceAccountEmail As String = "console-generated-account-here@developer.gserviceaccount.com"
Public ConsoleApplicationName As String = "I just realized this may be my issue. Not sure what this value should be!"

Public Function VerifySubscriptionReceipt(ByRef expirationDate As DateTime,
                                          ByRef purchaseDate As DateTime,
                                          token As String,
                                          purchasedProductID As String,
                                          signature As String,
                                          rawJSONData As String) As Boolean?

    'First: basic check to verify the receipt matches the product you will deliver
    If Not purchasedProductID = InAppPurchaseProductID Then
        Return Nothing
    End If

    'Second: verify receipt signature
    Dim rkp As RsaKeyParameters = CType(PublicKeyFactory.CreateKey(Convert.FromBase64String(PublicLicenseKey)), RsaKeyParameters)
    Dim rsaParams As New RSAParameters
    rsaParams.Modulus = rkp.Modulus.ToByteArrayUnsigned
    rsaParams.Exponent = rkp.Exponent.ToByteArrayUnsigned
    Dim rsaProvider As New RSACryptoServiceProvider
    rsaProvider.ImportParameters(rsaParams)

    If Not rsaProvider.VerifyData(Encoding.UTF8.GetBytes(rawJSONData), CryptoConfig.MapNameToOID("SHA1"), Convert.FromBase64String(signature)) Then
        Return Nothing
    End If

    'Third: check subscription status
    Dim result As SubscriptionPurchase
    Dim serviceInitializer As New AndroidPublisherService.Initializer With
        {
            .HttpClientInitializer = New ServiceAccountCredential(New ServiceAccountCredential.Initializer(ConsoleServiceAccountEmail) With
                                                                  {
                                                                      .Scopes = New String() {"https://www.googleapis.com/auth/androidpublisher"}
                                                                  }.FromCertificate(ConsoleCertificate)),
            .ApplicationName = ConsoleApplicationName
        }

    Using service As New AndroidPublisherService(serviceInitializer)

        '*** Error thrown on this line. Google returning 403 Forbidden ***
        result = service.Purchases.Get(PackageName, InAppPurchaseProductID, token).Execute

    End Using

    If Not result.AutoRenewing.HasValue And
            result.ValidUntilTimestampMsec.HasValue And
            result.InitiationTimestampMsec.HasValue Then
        Throw New KeyNotFoundException("The subscription does not appear to be valid. (token: " & token & ")")
    End If

    expirationDate = New DateTime(result.ValidUntilTimestampMsec.Value)
    purchaseDate = New DateTime(result.InitiationTimestampMsec.Value)

    Return result.AutoRenewing.Value

End Function

所以在查看示例代码时,我意识到可能的问题是我真的不知道ConsoleApplicationName 应该是什么。我曾尝试使用 Google Developer Console 中的“项目名称”和“项目 ID”,但都没有任何区别。

代码一直运行,直到 Google 返回 403 异常。

【问题讨论】:

  • 我同意尽管有很多关于各种 Google api 的文档,但它确实感觉有点零碎,并且很难确定所有适当的步骤来进行 oauth2 身份验证和请求结束结尾。让我知道你是否有进一步的运气。
  • 嗨,我不是这个 API 的专家(我实际上从未使用过它),但我绝对是你为 Google API 提供 .NET 客户端库的人 :) 重新升级 OAuth 2.0 协议- 我建议您阅读以下页面 - developers.google.com/api-client-library/dotnet/guide/aaa_oauth。你能详细说明一下签名过程吗?同时,我会联系 Google Play Android Developer API 的所有者。你也应该看看这个页面:developers.google.com/android-publisher/getting_started
  • 感谢您回复我。我认为我在客户端库方面取得了一些进展。安装过程的一个建议 - 我认为应该将 Google.Apis.Auth 设置为 nuget 依赖项。大多数情况下,我认为 android-publisher 库的文档需要很多帮助。它似乎在服务帐户和 Web 帐户之间来回切换,如果让它从头到尾遵循每个选项并为两者提供用例会更有用。就我而言,我想我发现我需要使用服务帐户。
  • 我还没有进行太多测试,但假设我现在可以使用客户端库调用 Get / Cancel 到购买状态 API,这将处理上面的 #3。不过,我找不到任何有关 Android 商店返回的验证签名格式的文档。它只是公共许可证密钥,我可以进行基本的字符串比较吗?还是使用公钥对订单数据进行某种散列?我的计划是先以某种方式验证签名,然后使用客户端库检查状态,如果两者都很好,则交付产品。听起来对吗?
  • @peleyal 我从developers.google.com/api-client-library/dotnet/guide/… 使用的代码似乎在我的本地开发机器上运行,但在Windows Azure 上却不行,因为var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable); 行会引发错误。经过一番研究,我发现在 Azure 上,我必须使用X509KeyStorageFlags.MachineKeySet。但后来我收到以下错误:

标签: asp.net-web-api google-api google-api-dotnet-client


【解决方案1】:

这篇相关帖子解决了我的 403 错误问题:https://stackoverflow.com/a/23029201/2092250

它现在正在工作。至于 ConsoleApplicationName - 我什至不确定它是否是必填字段。可能会被完全忽略。这个值似乎无关紧要。

我必须说,Google 不可能让这件事变得更混乱或更糟糕的记录。但它终于起作用了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 2018-09-24
    • 2020-01-07
    • 2012-06-22
    相关资源
    最近更新 更多