如何使用 .NET 的 Google 客户端 API 获取解码的 Play Integrity API 令牌

How to get a decoded Play Integrity API token using the Google Client API for .NET

我正在尝试使用 Google.Apis.PlayIntegrity.v1 namespace of the Google Client API library for .NET to decode an integrity token, as recommended in the Play Integrity API documentation

但是,库文档中并不清楚如何执行此操作。似乎解码后的令牌有效负载应该从 DecodeIntegrityTokenResponse 对象返回,但我找不到任何 returns 这种类型的方法。

我希望这种类型会从 DecodeIntegrityToken(DecodeIntegrityTokenRequest, String) 返回,但这种方法实际上 returns 另一个 DecodeIntegrityTokenRequest,这根本没有帮助。

有人成功使用这个库来解码令牌吗?

参考文献:

尝试的代码:

String integrityToken = "...token...";

String serviceAccountEmail = "...service account email...";

var certificate = new X509Certificate2("...key.p12...", "...secret...");

ServiceAccountCredential credential = new ServiceAccountCredential(
     new ServiceAccountCredential.Initializer(serviceAccountEmail)
     {
             Scopes = new[] { PlayIntegrityService.Scope.Playintegrity }
     }.FromCertificate(certificate));

// Create the service.
var service = new PlayIntegrityService(new BaseClientService.Initializer()
{
        HttpClientInitializer = credential,
        ApplicationName = "Play Integrity API Sample",
});

DecodeIntegrityTokenRequest request = new     DecodeIntegrityTokenRequest();
request.IntegrityToken = integrityToken;
DecodeIntegrityTokenResponse response = service.V1.DecodeIntegrityToken(request, "...package name...");

Error CS0029 Cannot implicitly convert type 'Google.Apis.PlayIntegrity.v1.V1Resource.DecodeIntegrityTokenRequest' to 'Google.Apis.PlayIntegrity.v1.Data.DecodeIntegrityTokenResponse'

你应该依靠你的 ide 来告诉你这些对象是什么类型

您的代码表明方法 DecodeIntegrityToken 应该 return 一个 DecodeIntegrityTokenResponse。您是这样声明它而不是使用 var 并允许编译器为您计算出来。

DecodeIntegrityTokenResponse response = service.V1.DecodeIntegrityToken(request, "...package name...");

你的问题是这个方法实际上return是一个DecodeIntegrityTokenRequest

要获得 DecodeIntegrityTokenResponse 您需要执行请求

var request = service.V1.DecodeIntegrityToken(requestBody, "...package name...");
var result = request.Execute();

完整代码清理

using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using Google.Apis.PlayIntegrity.v1;
using Google.Apis.PlayIntegrity.v1.Data;
using Google.Apis.Services;

Console.WriteLine("Hello, World!");


var integrityToken = "...token...";

var serviceAccountEmail = "...service account email...";
var pathToCert = "...key.p12...";
var certPassword = "...secret...";
var packageName = "...package name...";

var certificate = new X509Certificate2(pathToCert, certPassword);

var credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
        Scopes = new[] { PlayIntegrityService.Scope.Playintegrity }
    }.FromCertificate(certificate));

// Create the service.
var service = new PlayIntegrityService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "Play Integrity API Sample",
});

var requestBody = new DecodeIntegrityTokenRequest
{
    IntegrityToken = integrityToken
};
var request = service.V1.DecodeIntegrityToken(requestBody, packageName);

var result = request.Execute();