如何在Flutter中获取Wordpress API

时间:2019-02-14 12:02:05

标签: flutter woocommerce-rest-api

api() async {

    var method = "GET";

    final String BASE_SITE = "mysite.com";

    final String BASE_URL = "http://" + BASE_SITE + "/wp-json/wc/v3/products/categories";
    final String COSTUMER_KEY = "ck_8f001a7594f3f1c92fcb6f9bd7b74581b59622e7";
    String COSTUMER_SECRET = "cs_2f9e22777f6a8169eda115f15b761a9075075d46";

    var nonce = random.randomNumeric(10);
    var time = DateTime.now().millisecondsSinceEpoch.toString();
    print("Nonce "+nonce);
    print("Time "+time);

    String firstEncodedString = method + "&" + Uri.encodeComponent(BASE_URL);

    String parameterString = "oauth_consumer_key=" + COSTUMER_KEY + "&oauth_nonce=" + nonce + "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + time + "&oauth_version=1.0";
    String secoundEncodedString = "&" + Uri.encodeComponent(parameterString);

    String baseString = firstEncodedString + secoundEncodedString;

    var key = utf8.encode(baseString );
    var bytes = utf8.encode(COSTUMER_SECRET);

    var hmac = new Hmac(sha1, key); // HMAC-SHA1
    //var digest = hmac.convert(bytes);

    //print("digest "+Uri.encodeComponent(digest.toString()));


    var signature = Uri.encodeComponent(base64Encode(hmac.convert(bytes).bytes));

    String parseUrl = BASE_URL + "?" + "&oauth_signature_method=HMAC-SHA1&oauth_consumer_key=" + COSTUMER_KEY + "&oauth_version=1.0&oauth_timestamp=" + time + "&oauth_nonce=" + nonce + "&oauth_signature=" + signature;
    print(parseUrl);
    final response =
    await http.get(parseUrl);
    setState(() {

      isLoading = false;
    });
    print("REsponse "+response.body.toString());

  }

我已阅读woo-commerce api指南以从服务器获取数据。我正在使用上面的代码来实现此目的,但这给我一个错误,签名数据不匹配。为什么?我在这里做错了。

0 个答案:

没有答案