首页 > 解决方案 > 正方形 INVALID_REQUEST_ERROR

问题描述

我正在玩 Flutter 和 Square Sandbox 并尝试从应用程序中收取信用卡费用。

这是我作为生成的 cURL 输出得到的:

curl --request POST https://connect.squareup.com/v2/locations/MY_LOCATION_ID/transactions \--header "Content-Type: application/json" \--header "Authorization: Bearer API_TOKEN" \--header "Accept: application/json" \--data '{"idempotency_key": "","amount_money": {"amount": 100,"currency": "USD"},"card_nonce": "cnon:CNONID"}'

但是我收到此错误:

{"errors":[{"category":"AUTHENTICATION_ERROR","code":"UNAUTHORIZED","detail":"This request could not be authorized."}]}

我插入位置 ID 和 API 密钥,但这仍然是我得到的响应。知道为什么吗?我需要安装一些 SSL 证书还是什么?

编辑

一段时间后,我尝试了这个:

String chargeServerHost = "http://192.168.178.35:8000";
String chargeUrl = "$chargeServerHost/buyCoins";

class ChargeException implements Exception {
  String errorMessage;
  ChargeException(this.errorMessage);
}

Future<void> chargeCard(CardDetails result) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  String token = prefs.getString('token');

  var body = jsonEncode({"nonce": result.nonce, "token" : token});

  http.Response response;
  try {
    response = await http.post(chargeUrl, body: body, headers: {
      "Accept": "application/json",
      "content-type": "application/json"
    });
  } on SocketException catch (ex) {
    throw ChargeException(ex.message);
  }

  var responseBody = json.decode(response.body);
  print(responseBody.toString());

  if (response.statusCode == 200) {
    return;
  } else {
    throw ChargeException(responseBody["errorMessage"]);
  }
}

我得到了 nonce 值,但作为回应,我得到了这个:

{errors: [{code: NOT_FOUND, detail: Card nonce not found, category: INVALID_REQUEST_ERROR}]}

即使我有随机数的价值......这是卡输入后生成的一些随机字符串。

标签: curlflutterdartsquare

解决方案


推荐阅读