cancel
Showing results for 
Search instead for 
Did you mean: 

create user from java code

binduj
Champ in-the-making
Champ in-the-making
Hi,

I'm trying to create a new alfresco user from our java application using
http://wiki.alfresco.com/wiki/Repository_RESTful_API_Reference#Add_Person

The way Im trying is
1. first log-in as user(who has admin privileges). This returns an alf_ticket.
2. Using the ticket, try to invoke the Add person request.

When I tried as above, Im getting a 401 Authentication error. More details below:

This is the post request we are sending:-

post=POST /alfresco/service/api/people?alf_ticket=TICKET_07eb3b3751b5d493df751c7c473776f604f23791 HTTP/1.1
Host: 192.168.225.82
Authorization: Basic YWRtaW46bVVmYXo2aVA=
User-Agent: HPi Sync
Connection: close
Content-Type: application/json
Content-Length: 119

{"password" : "pasword", "userName" : "50000", "email" : "a@b.com", "firstName" : "firstName", "lastName" : "lastName"}
This is the error we got:-

response=HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json;charset=UTF-8
Date: Fri, 29 Apr 2011 06:13:01 GMT
Connection: close


  "status" :
  {
    "code" : 401,
    "name" : "Unauthorized",
    "description" : "The request requires HTTP authentication."
  }, 
 
  "message" : "03280002 Web Script org\/alfresco\/repository\/person\/people.post requires admin authentication; however, a non-admin has attempted access.", 
  "exception" : "org.springframework.extensions.webscripts.WebScriptException - 03280002 Web Script org\/alfresco\/repository\/person\/people.post requires admin authentication; however, a non-admin has attempted access.",


Not sure if this is the right approach that I should follow. Please suggest.
Or provide me some sample java code that I can use to create a new user.
1 REPLY 1

jpotts
World-Class Innovator
World-Class Innovator
I suspect something is wrong with your header. I used your JSON and posted successfully against a 3.3 Enterprise server using curl. I used both the ticket method you described as well as just telling curl the username and password of my admin user.

Notice that when I use the ticket, my authorization header does not get set whereas in your case it does. Maybe that is a clue.

Posting a user with a ticket:
jpotts-alfresco-mbp:~ jpotts$ curl -v -X POST "http://localhost:8080/alfresco/service/api/people?alf_ticket=TICKET_7284f7992fb43f9bd34ba25e8e5a49f7..." -d@./temp.json -H "Content-type:application/json"
* About to connect() to localhost port 8080 (#0)
*   Trying ::1… connected
* Connected to localhost (::1) port 8080 (#0)
> POST /alfresco/service/api/people?alf_ticket=TICKET_7284f7992fb43f9bd34ba25e8e5a49f79acd5ec4 HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:8080
> Accept: */*
> Content-type:application/json
> Content-Length: 120
>
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Set-Cookie: JSESSIONID=2041120B8E555098B65351A91D15F890; Path=/alfresco
< Cache-Control: no-cache
< Pragma: no-cache
< Content-Type: application/json;charset=UTF-8
< Content-Length: 590
< Date: Wed, 15 Feb 2012 21:26:16 GMT
<
{
   "url": "\/alfresco\/service\/api\/person\/50002",
   "userName": "50002",
   "enabled": true,
   "firstName": "firstName",
   "lastName": "lastName2",
   "jobtitle": null,
   "organization": null,
   "location": null,
   "telephone": null,
   "mobile": null,
   "email": "a@b.com",
   "companyaddress1": null,
   "companyaddress2": null,
   "companyaddress3": null,
   "companypostcode": null,
   "companytelephone": null,
   "companyfax": null,
   "companyemail": null,
   "skype": null,
   "instantmsg": null,
   "googleusername": null,
   "quota": -1,
   "sizeCurrent": 0,
   "persondescription": null
}
* Connection #0 to host localhost left intact
* Closing connection #0

Posting a user by passing in the username and password:
jpotts-alfresco-mbp:~ jpotts$ curl -v -uadmin:admin -X POST "http://localhost:8080/alfresco/service/api/people" -d@./temp.json -H "Content-type:application/json"
* About to connect() to localhost port 8080 (#0)
*   Trying ::1… connected
* Connected to localhost (::1) port 8080 (#0)
* Server auth using Basic with user 'admin'
> POST /alfresco/service/api/people HTTP/1.1
> Authorization: Basic YWRtaW46YWRtaW4=
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:8080
> Accept: */*
> Content-type:application/json
> Content-Length: 120
>
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Set-Cookie: JSESSIONID=A4900FD9AE3C9E97FCCCA5F7458E1A51; Path=/alfresco
< Cache-Control: no-cache
< Pragma: no-cache
< Content-Type: application/json;charset=UTF-8
< Content-Length: 590
< Date: Wed, 15 Feb 2012 21:24:22 GMT
<
{
   "url": "\/alfresco\/service\/api\/person\/50001",
   "userName": "50001",
   "enabled": true,
   "firstName": "firstName",
   "lastName": "lastName1",
   "jobtitle": null,
   "organization": null,
   "location": null,
   "telephone": null,
   "mobile": null,
   "email": "a@b.com",
   "companyaddress1": null,
   "companyaddress2": null,
   "companyaddress3": null,
   "companypostcode": null,
   "companytelephone": null,
   "companyfax": null,
   "companyemail": null,
   "skype": null,
   "instantmsg": null,
   "googleusername": null,
   "quota": -1,
   "sizeCurrent": 0,
   "persondescription": null
}
* Connection #0 to host localhost left intact
* Closing connection #0

Jeff