Fórum

JSON - No User exists with the primary key 0

thumbnail
Jan Beran, modificado 8 Anos atrás.

JSON - No User exists with the primary key 0

Junior Member Postagens: 44 Data de Entrada: 30/06/15 Postagens Recentes
Hi all

So, I am creating this Liferay portal content in my job. Part of the task is to create a large number of dynamic data lists, that will change frequently. Since Liferay does not support bulk import of DDL data and manual input is just impractical, I decided to use the JSON Web service functionality, namely the HTTP request. I encountered several problems along the way.

1) There is a nice bug in the JSON web interface. When you input the parameters and generate the HTTP request, its is missing a single letter, namely in the parameter record-set-i D . This generates the "No JSON web service action associated with path XXX and method GET for /" exception.
I trusted you Liferay, and you let me down emoticon

2) There is no official documentation on how to construct the security element of the HTTP request. Incorrect element generates the "Authenticated access required" exception. The answer to this can be found here:
http://www.liferaysavvy.com/2015/03/liferay-json-web-services-authenticated.html
Kudos to Meera for that blog post.

3) The previous bugs I overcame with a combination of googling and tinkering. But then I came to the "No User exists with the primary key 0" exception for the /ddlrecord/add-record method. I found on the web, this might have something to do with how Liferay detects the user. Namely, it does not pass the user identificator from the header, which remains null, or 0. The DDL record is than not created, since it requires an author.

So far, I don't know how to solve this. Do you have any ideas?
thumbnail
James Falkner, modificado 8 Anos atrás.

RE: JSON - No User exists with the primary key 0

Liferay Legend Postagens: 1399 Data de Entrada: 17/09/10 Postagens Recentes
Jan Beran:
Hi all
1) There is a nice bug in the JSON web interface. When you input the parameters and generate the HTTP request, its is missing a single letter, namely in the parameter record-set-i D . This generates the "No JSON web service action associated with path XXX and method GET for /" exception.
I trusted you Liferay, and you let me down emoticon

See LPS-50384 *sad face*


2) There is no official documentation on how to construct the security element of the HTTP request. Incorrect element generates the "Authenticated access required" exception. The answer to this can be found here:
http://www.liferaysavvy.com/2015/03/liferay-json-web-services-authenticated.html
Kudos to Meera for that blog post.


Liferay supports industry-standard authentication techniques, one of which is HTTP Basic Authentication emoticon There is also a wikipedia page on it! But basically you have to authenticate for most JSON web services, especially those that change the database.



3) The previous bugs I overcame with a combination of googling and tinkering. But then I came to the "No User exists with the primary key 0" exception for the /ddlrecord/add-record method. I found on the web, this might have something to do with how Liferay detects the user. Namely, it does not pass the user identificator from the header, which remains null, or 0. The DDL record is than not created, since it requires an author.

So far, I don't know how to solve this. Do you have any ideas?



You are probably not constructing the Basic auth header correctly. Does it work with a known-to-work tool, like the unix/linux command line "curl"? For example, on Liferay 6.2 I just created a simple DDL definition using the GUI that contains two text fields (text1, text2). Then used the JSON console to create a new record, and revealed the curl command which works for me:

curl https://my-server.com/api/jsonws/ddlrecord/add-record -u test@liferay.com:MYPASSWORD -d groupId=10182 -d recordSetId=70006 -d displayIndex=0 -d 'fieldsMap={"text1":"foo", "text2": "bar"}'


Ensure you pass the proper groupId and recordSetId (are you?)

This created a new record where text1=foo, text2=bar. Using curl -v revealed the proper header value that it constructed (along with all the other stuff, including verification that I was doing an HTTP POST vs. GET):

POST /api/jsonws/ddlrecord/add-record HTTP/1.1
Authorization: Basic dGVzdEBsaWZlcmF5LmNvbTpNWVBBU1NXT1JE
User-Agent: curl/7.37.1
Host: my-server.liferay.com
Accept: */*
Content-Length: 88
Content-Type: application/x-www-form-urlencoded
thumbnail
Jan Beran, modificado 8 Anos atrás.

RE: JSON - No User exists with the primary key 0

Junior Member Postagens: 44 Data de Entrada: 30/06/15 Postagens Recentes
Hi James

Thanks for the prompt reply. My problem is, I have no know-to-work tool. The JSON web API, HTTP request and most recently the CURL command return the same exception.

I am also sure about the groupID and recordsetID. I forgot to mention, the SET request work just fine, and I was able to get these through the /ddlrecordset/get-record-set. That also mean my authentication header is correct, right?

I am pretty sure the problem lies somewhere on the server side. If you have any other ideas, I'd love to hear them?
thumbnail
James Falkner, modificado 8 Anos atrás.

RE: JSON - No User exists with the primary key 0

Liferay Legend Postagens: 1399 Data de Entrada: 17/09/10 Postagens Recentes
Are you on 6.2? CE? EE? Lots of stuff changed in JSON WS in 6.2.

Can you also check the permissions on the DDL Data Definition and DDLRecordSet? That is, in the UI, on the Data Definition, ensure that the user you are authenticating as has appropriate permissions to add stuff. Same with the RecordSet too. You can just login using the same user via the UI and add entries to the DDL.
thumbnail
Jan Beran, modificado 8 Anos atrás.

RE: JSON - No User exists with the primary key 0

Junior Member Postagens: 44 Data de Entrada: 30/06/15 Postagens Recentes
Yes, I am indeed using Liferay 6.2 CE.

I am trying to write to a DDL that I have created and am able to put records in, both in the UI. Therefore, I have permission enough.
thumbnail
James Falkner, modificado 8 Anos atrás.

RE: JSON - No User exists with the primary key 0

Liferay Legend Postagens: 1399 Data de Entrada: 17/09/10 Postagens Recentes
Jan Beran:
Yes, I am indeed using Liferay 6.2 CE.

I am trying to write to a DDL that I have created and am able to put records in, both in the UI. Therefore, I have permission enough.


Is your portal set to use email address (the default) when logging in? If not, then you have to construct the Authorization header using whatever method (screenname for example) you normally use to sign in using the UI.

Any other settings that you might have in a custom portal-ext.properties that might change the way Liferay authenticates or change some JSON web service setting?

If that checks out, then I'd ask you to re-download Liferay Portal 6.2 CE GA4, start it up, and create a super simple DDL definition and DDL, and then try to insert records using that. This worked for me the first and every time using 'test@liferay.com / test' and with no custom portal-ext.properties (and using HSQLDemoticon. You may also try to re-index the search indexes, using Control Panel -> Server Administration -> Reindex all search indexes (Click exceute). Perhaps your user database index is wrong somehow.
thumbnail
Jan Beran, modificado 8 Anos atrás.

RE: JSON - No User exists with the primary key 0

Junior Member Postagens: 44 Data de Entrada: 30/06/15 Postagens Recentes
I am indeed logging in through the UI with the email address, as well as through the HTTP/Curl request. This should be no problem. I have also tried the reindexing and switching on/off the JAAS, both to no avail.

What portal-ext.properties that might change the way Liferay authenticates do you have in mind?

Anyway, I need to stress, that some of the HTTP requests are working, like for example the GET-DDLRECORD one, so the authorization of the request should be OK.
thumbnail
Jan Beran, modificado 8 Anos atrás.

RE: JSON - No User exists with the primary key 0

Junior Member Postagens: 44 Data de Entrada: 30/06/15 Postagens Recentes
Hi again. Been some time, I had vacations and other projects to work on. Anyway, I still have not resolved the problem.

So, I still cannot use the ddlrecord/add-record method, since it crashed with: {"exception: No User exists with the primary key 0"}

Also, when trying the use the add-kb-article method, I am given the: {"exception": "com.liferay.portal.security.auth.PrincipalException"}
This should mean, that I don't have rights to add KB articles (which I do). Could be the same problem?

Funny thing is, both ddlrecord/delete-record and delete-kb-article methods are working just fine.

This is the relevant part of the portal-ext.properties:

company.security.auth.requires.https=true
session.enable.phishing.protection=false
web.server.protocol=https

json.web.service.enabled=true
json.service.auth.token.hosts.allowed=127.0.0.1,IP
json.service.auth.token.enabled=false
jsonws.web.service.public.methods=*
jsonws.servlet.hosts.allowed=127.0.0.1,IP

portal.jaas.enable=true


I have also fired up a clean portal installation, where everything is working as intended, so the problem is NOT in the request formula or the authentication header.
thumbnail
Jan Beran, modificado 8 Anos atrás.

RE: JSON - No User exists with the primary key 0

Junior Member Postagens: 44 Data de Entrada: 30/06/15 Postagens Recentes
So, I still dont know what is the cause of the problem for me, but I might have found a solution. Point is, the production server I am working with is running on Liferay 6.2 GA3. So far, I was experimenting with localhost running at GE4. I was able to inload the required data there and then tried to export/import them to the production environment. This is not possible, since they have different builts.

OK, OK, I fire up localhost running at GA3, try the JSON call and voila, I get the same bug: No User exists with the primary key 0
And that was on a clean server instance with absolutely no settings.

Can someone confirm this for me? James? Are you still there?
thumbnail
Allen Ziegenfus, modificado 8 Anos atrás.

RE: JSON - No User exists with the primary key 0

New Member Postagens: 11 Data de Entrada: 29/12/15 Postagens Recentes
Hi Jan,

Did you ever find a fix for this problem? I am getting the same issue on 6.1 with adding ddl records. But as you also mentioned, with the same credentials I can perform other requests like create ddl record sets...

Allen
thumbnail
Allen Ziegenfus, modificado 8 Anos atrás.

RE: JSON - No User exists with the primary key 0

New Member Postagens: 11 Data de Entrada: 29/12/15 Postagens Recentes
Actually I was able to get this to work on my side.

The problem does not have to do with authentication, but with how the userId is handled. The addrecord method requires the user id, but the other methods you mentioned (like deleterecord) do not take the user id.

I found that by adding the correct userId to the serviceContext it is possible to resolve the issue and create DDL records, e.g. like this:

curl https://$SERVER/api/secure/jsonws/ddlrecord/add-record \
-u $USER:$PASSWORD \
-d serviceContext.userId=$USERID \
-d groupId=$GROUPID \
-d recordSetId=$RECORDSETID \
-d displayIndex=0 \
-d fieldsMap="{'link_name': 'test'}" \
-d scope=0

Not sure when this is necessary as James reported that this was working for him, so it might have been fixed at some point. But maybe it will help anyone else struggling with this issue.