首页 > 解决方案 > 我如何将googleapis与flutter firebase一起使用

问题描述

我正在使用颤振 + firebase auth firestore 等,我想使用Purchases.products: get但解释一点用都没有 我不明白如何进行身份验证

我的代码

var url = 'https://www.googleapis.com/auth/androidpublisher/v3/applications/'
        '${purchaseDetails.billingClientPurchase.packageName}/purchases/products/'
        '${purchaseDetails.productID}/tokens/'
        '${purchaseDetails.billingClientPurchase.purchaseToken}';
    var response = await http.get(url);
    if (response.statusCode == 200) {
      var jsonResponse = jsonDecode(response.body);
      print(jsonDecode);
    } else {
      print('Request failed with status: ${response.statusCode}.');
    }

标签: firebaseflutteroauth-2.0google-apifirebase-authentication

解决方案


虽然在我找到实验的任何地方都没有健康的文档,但我将它分享给有需要的人

您需要获得 2 个包googleapis_authgoogleapis

import 'package:googleapis/androidpublisher/v3.dart' as androidPublisher;
import 'package:googleapis_auth/auth_io.dart' as auth;

final _credentials = new auth.ServiceAccountCredentials.fromJson(r'''
      {
        "private_key_id": ...,
        "private_key": ...,
        "client_email": ...,
        "client_id": ...,
        "type": "service_account"
      }
      ''');

    const _SCOPES = const [
      androidPublisher.AndroidpublisherApi.AndroidpublisherScope
    ];

    auth.clientViaServiceAccount(_credentials, _SCOPES).then((httpClient) {
      var publisher = new androidPublisher.AndroidpublisherApi(httpClient);
      publisher.purchases.products
          .get(
              packageName,
              productID,
              purchaseToken)
          .then((pub) {
        debugPrint(pub.toJson().toString());
      });
    });

你可以创建这样的键

Open the IAM & Admin page in the Cloud Console.

Open the IAM & Admin page

Click Select a project, choose a project, and click Open.

In the left nav, click Service accounts.

Find the row of the service account that you want to create a key for. In that row, click the More more_vert button, and then click Create key.

Select a Key type and click Create.

推荐阅读