Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

401 unauthorized error #62

Open
Hunter031 opened this issue Dec 26, 2020 · 2 comments
Open

401 unauthorized error #62

Hunter031 opened this issue Dec 26, 2020 · 2 comments

Comments

@Hunter031
Copy link

I am trying to play around with the API methods to create and get users. But I keep getting 401 Unauthorized error. I tried with the admin ID and password but I am not sure why I get 401 unauthorized error. Below is my code and request and response log. I have replaced my actual user id and pwd with *** for security purposes.. Pls advise.

09:49:13.490 [main] DEBUG c.afrozaar.wordpress.wpapi.v2.Client - Request Entity: <GET https://cs.genoo.com?rest_route=/wp/v2/users&context=view,{Authorization=[Basic R24zc2lQMDpGRjR4QFZkS3otLTJIWWlzWkM=], User-Agent=[wp-api-v2-client-java/4.8.1]}>
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 401 Unauthorized

public class WordPressAPI {

public static void main (String args[]) {
	com.afrozaar.wordpress.wpapi.v2.Wordpress wordpress = getWordpressClient();
	System.out.println(wordpress);
	
	User user = new User();
	List<User> users = new ArrayList<User>();
	user.setEmail("hunter@gen.com");
	user.setName("Name");
	/*try {
	  user = wordpress.createUser(user, "hunter@gen.com", 
			  "test");
	 users = wordpress.getUsers();
	 System.out.println("Users ==" + users);
	} catch (UsernameAlreadyExistsException e) {
	  e.printStackTrace();
	} catch (UserEmailAlreadyExistsException e) {
	  e.printStackTrace();
	} catch (WpApiParsedException e) {
		e.printStackTrace();
	}*/
	
	 users = wordpress.getUsers();
	 System.out.println("Users ==" + users);
	 
	Term term = new Term();
	term.setName("Test Hunter");
	term.setSlug("Delete this entry");
	Term cat = wordpress.createCategory(term);
	System.out.println(cat);
}

public static com.afrozaar.wordpress.wpapi.v2.Wordpress getWordpressClient() {
    String baseUrl = "https://cs.genoo.com";
    String username = "*****";
    String password = "*****";
    return ClientFactory
      .fromConfig(ClientConfig.of(baseUrl, username, password,
          false,
          true));
}
@fo0
Copy link

fo0 commented Apr 4, 2021

I am getting the same error / problem, any ideas?
Using Wordpress 5.x (latest) without any installed plugin

With the following error message

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wpClient': 
Invocation of init method failed; nested exception is org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized: 

[{"code":"rest_user_cannot_view","message":"Sorry, you are not allowed to list users.","data":{"status":401}}]

Update: Ok, got it. Some plugins just blocked my requests... AND i had to install manually the rest basic auth plugin from git https://github.com/WP-API/Basic-Auth

@nithyasharabheshwara
Copy link

Thanks this was really helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants