Start a new topic

How to create a user using Kinvey java Api?

Here is my code:



public class SignUp {

public static final String appKey = "kid_Pnbmbmbnbm";





public static void main(String[] args){

SignUp obj=new SignUp();

Client mKinveyClient=SignUp.getClient();



obj.signup(mKinveyClient, "abc", "abc");

//obj.updateUser(mKinveyClient, "xyz", "xyz");

//obj.deleteUser(mKinveyClient, "xyz", "xyz");

}





public User signup(Client mKinveyClient,String userName,String password) {

User result=null;

try{

result = (User) mKinveyClient.user().createBlocking(userName, password).execute();

} catch (IOException e) {

System.out.println("Couldn't create -> " + e.getMessage());

}

return result;

}





public User updateUser(Client mKinveyClient,String userName,String password) {

User result=null;

mKinveyClient.user().put("fav_food", "bacon");

try{

result = (User) mKinveyClient.user().updateBlocking().execute();

} catch (IOException e) {

System.out.println("Couldn't create -> " + e.getMessage());

}

return result;

}



public void deleteUser(Client mKinveyClient,String userName,String password) {



try{

mKinveyClient.user().deleteBlocking(true).execute();

}catch (IOException e) {

System.out.println("Couldn't create -> " + e.getMessage());

}



}

public static Client getClient(){

Client myJavaClient = new Client.Builder(appKey, mastersecret).build();

return myJavaClient;





}





}



i am geting msg "Unknown" .I am unable to do any operation using the code given in User java guide

Please help.



Also want to know is there any sample application acting as a middlelayer and deployed on Kinvey cloud.Just like we see in Google app engine.

We are exploring how we can build a middle-layer using kinvey API and integrate with our mobile app.

Hey,



at first glance all that looks correct. Can you try logging some of the additional information from the throwable error? That might help us figure out what's going on.





...

catch (IOException e) {

System.out.println("Couldn't create -> " + e.getMessage());

e.printStackTrace();

}



I wanted to let you know I have edited your post to remove the Master Secret.
Thanks for responding.



Here is the stackTrace:



Couldn't create -> unknown

com.kinvey.java.core.KinveyJsonResponseException: unknown

at com.kinvey.java.core.KinveyJsonResponseException.from(KinveyJsonResponseException.java:87)

at com.kinvey.java.auth.KinveyAuthRequest.executeUnparsed(KinveyAuthRequest.java:204)

at com.kinvey.java.auth.KinveyAuthRequest.execute(KinveyAuthRequest.java:208)

at com.kinvey.java.User$LoginRequest.execute(User.java:590)

at com.gl.kinvey.sample.SignUp.signup(SignUp.java:25)

at com.gl.kinvey.sample.SignUp.main(SignUp.java:16)
Hey,



Can you visit the User section of the console and check to see if that user already exists? I'm wondering if there is a bug in the java library where this specific error is not being handled properly, which is why you are seeing `unknown`



Note that you cannot create two users with the same username, so the second attempt will fail.



For the current console, check here for a user with username "abc": https://console.kinvey.com/addons/users
Hi,

The user with the name "abc" is not added in User section.
Hey,



Can you check out the troubleshooting guide and enable debug logging?



http://devcenter.kinvey.com/java/guides/troubleshooting



This will output the raw http requests and responses to the logs.



Also, can you try to use myClient.ping().execute() and make sure that is working successfully?
Here is the log:



Sep 01, 2014 1:37:13 PM com.google.api.client.http.HttpRequest execute

CONFIG: -------------- REQUEST --------------

GET https://baas.kinvey.com/appdata/kid_PVetNnR5MU

Accept-Encoding: gzip

Authorization:

User-Agent: android-kinvey-http/2.6.12

x-kinvey-api-version: 3

x-kinvey-device-information: JAVA/1.7.0_25



Sep 01, 2014 1:37:14 PM com.google.api.client.http.HttpRequest execute

CONFIG: curl -v --compressed -H 'Accept-Encoding: gzip' -H 'Authorization: ' -H 'User-Agent: android-kinvey-http/2.6.12' -H 'x-kinvey-api-version: 3' -H 'x-kinvey-device-information: JAVA/1.7.0_25' -- 'https://baas.kinvey.com/appdata/kid_PVetNnR5MU'

Sep 01, 2014 1:37:15 PM com.google.api.client.http.HttpResponse

CONFIG: -------------- RESPONSE --------------

HTTP/1.1 200 OK

X-Kinvey-Request-Id: 5c01fe3859764e258641a73b42daf0d9

X-Kinvey-API-Version: 3

Date: Mon, 01 Sep 2014 08:07:15 GMT

Content-Length: 101

Connection: keep-alive

Content-Type: application/json; charset=utf-8

X-Powered-By: Express

Server: ngx_openresty



**ping---->true**

Sep 01, 2014 1:37:15 PM com.google.api.client.http.HttpRequest execute

CONFIG: -------------- REQUEST --------------

POST https://baas.kinvey.com/user/kid_PVetNnR5MU/

Accept-Encoding: gzip

Authorization:

User-Agent: android-kinvey-http/2.6.12

X-Kinvey-API-Version: 3

Content-Type: application/json; charset=UTF-8

Content-Length: 41



Sep 01, 2014 1:37:15 PM com.google.api.client.http.HttpRequest execute

CONFIG: curl -v --compressed -X POST -H 'Accept-Encoding: gzip' -H 'Authorization: ' -H 'User-Agent: android-kinvey-http/2.6.12' -H 'X-Kinvey-API-Version: 3' -H 'Content-Type: application/json; charset=UTF-8' -d '@-' -- 'https://baas.kinvey.com/user/kid_PVetNnR5MU/'
Sep 01, 2014 1:37:16 PM com.google.api.client.util.LoggingByteArrayOutputStream close

CONFIG: Total: 41 bytes

Sep 01, 2014 1:37:16 PM com.google.api.client.util.LoggingByteArrayOutputStream close

CONFIG: {"password":"rohi1t","username":"rohi1t"}

Sep 01, 2014 1:37:16 PM com.google.api.client.http.HttpResponse

CONFIG: -------------- RESPONSE --------------

HTTP/1.1 401 Unauthorized

X-Kinvey-Request-Id: 7e9d535705a8422787917a77d8b95a09

WWW-Authenticate: Basic realm="Authorization needed"

X-Kinvey-API-Version: 3

Date: Mon, 01 Sep 2014 08:07:16 GMT

Content-Length: 287

Connection: keep-alive

Content-Type: application/json; charset=utf-8

X-Powered-By: Express

Server: ngx_openresty



Couldn't create -> unknown

com.kinvey.java.core.KinveyJsonResponseException: unknown

at com.kinvey.java.core.KinveyJsonResponseException.from(KinveyJsonResponseException.java:87)

at com.kinvey.java.auth.KinveyAuthRequest.executeUnparsed(KinveyAuthRequest.java:204)

at com.kinvey.java.auth.KinveyAuthRequest.execute(KinveyAuthRequest.java:208)

at com.kinvey.java.User$LoginRequest.execute(User.java:590)

at com.gl.kinvey.sample.SignUp.signup(SignUp.java:25)

at com.gl.kinvey.sample.SignUp.main(SignUp.java:16)

Hey,



the problem is `401 Unauthorized ... "Authorization needed"`



creating new users uses the app key and secret as credentials, which are set when the client is built.



Can you first double check the appkey and secret and compare them with the console?



Secondly, are you running this code on a server? or will it be on user's machines? If it is on a server, you can use the master secret -- but if it is being distributed, you should use the app secret.





Login or Signup to post a comment