Flutter use aws_signature_v4 for ProductAPI

50 views Asked by At

I am using the aws_signature and want to use it to getItems from the Product API. But I simply can not make it work...

I tried it with the following code:

  static Future<void> getProductFromAsin({
    required String asin,
    required AmazonProductAPIMarketplace marketPlaceDetails,
  }) async {
    // Create the signer instance with credentials.
    const signer = AWSSigV4Signer(
      credentialsProvider: AWSCredentialsProvider(
        AWSCredentials(
          Env.amazonProductApiAccessKey,
          Env.amazonProductApiSecretKey,
        ),
      ),
    );

// Specify the region for the Product Advertising API
    final region = marketPlaceDetails.region;
    final scope = AWSCredentialScope(
      region: region,
      service: const AWSService('ProductAdvertisingAPI'),
    );
    // Prepare the `getItems` request
    final request = AWSHttpRequest(
      method: AWSHttpMethod.post,
      uri: Uri.https(marketPlaceDetails.host, '/paapi5/getItems'),
      headers: const {
        AWSHeaders.target:
            'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems',
        AWSHeaders.contentType: 'application/json',
      },
      body: json.encode({
        "ItemIds": [asin],
        "Resources": [
          "ItemInfo.Title",
          "Offers.Listings.Price",
          "Images.Primary.Medium" // Request medium size for the primary image
        ],
        "PartnerTag": AffiliatePartners.amazonDe.id, // Your associate tag
        "PartnerType": "Associates", // Specify the partner type
        "Marketplace": marketPlaceDetails.name,
      }).codeUnits,
    );

    // Sign and send the HTTP request
    final signedRequest = await signer.sign(
      request,
      credentialScope: scope,
    );

    final createResponse = await signedRequest.send().response;
    final createStatus = createResponse.statusCode;
    safePrint(createStatus);

    return;
  }

But this always prints 404. And when looking into the createResponse it says:

Bad state: Stream had already been listend to.

What am I missing here? Let me know if you need more information.

0

There are 0 answers