skip to Main Content

I am buidling flutter woocommerce store app for customers. I want to create/register a new customer using Dio() and I am using WordPress Plugin JWT Authentication for WP REST API for authentication. Here if i want to access URL for Customers Its showing following response. URL= "https://example.com/wp-json/wc/v3/customers"

{
code: "woocommerce_rest_cannot_view",
message: "Sorry, you cannot list resources.",
data: {
status: 401
}
}

My Custutomer Model file is:

class CustomerModel {
  String email;
  String firstName;
  String lastName;
  String password;
  CustomerModel(
      {required this.email,
      required this.firstName,
      required this.lastName,
      required this.password});
  Map<String, dynamic> toJson() {
    Map<String, dynamic> map = {};
    map.addAll({
      'email': email,
      'first_name': firstName,
      'last_name': lastName,
      'password': password,
      'username': email
    });
    return map;
  }
}

My API Service Mothod to create customer is:

Future<bool> createCustomer(CustomerModel model) async {
    var authToken =
        base64.encode(utf8.encode(Config.key + ':' + Config.sceret));
    print(authToken);
    bool ret = false;
    try {
      print("${Config.url+ Config.customerURL}");
      var response = await Dio().post(Config.url + Config.customerURL,
          data: model.toJson(),
          options: new Options(headers: {
            HttpHeaders.authorizationHeader: 'Basic $authToken',
            HttpHeaders.contentTypeHeader: 'application/json'
          }));

      print(response.statusCode);
      if (response.statusCode == 201) {
        ret = true;
      }
    } on DioError catch (e) {
      if (e.response?.statusCode == 404) {
        ret = false;
      } else {
        ret = false;
      }
    }
    return ret;
  }

My model Perameter Data is in following format:

{
 email: [email protected],
 first_name: qasim,
 last_name: ali,
 password: qasim123,
 username: [email protected]
}

My createCustomer Method is always returning false ::: I mean Dio().Post() is not working successfully kindly guide

2

Answers


  1. Instead of Basic,

    HttpHeaders.authorizationHeader: 'Basic $authToken',
    

    Use Bearer in the header.

    HttpHeaders.authorizationHeader: 'Bearer $authToken',
    
    Login or Signup to reply.
  2. I have same issue and manage to resolve it by removing authorazation header and append the key and secret to the post path,I am just not sure if this is the safe way to implement it. see the code below

    var response = await Dio().post(Config.url + Config.customerURL + '?consumer_key=' + Config.key + '&consumer_secret=' + Config.secret,
        data:model.toJson(),
    
        options: Options(headers: {
          //HttpHeaders.authorizationHeader:'Bearer: $authToken',
          HttpHeaders.contentTypeHeader: 'application/json',
        },
    

    I added back the authorazation header and its working too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search