Token based rest service with username and password

4112
5
Jump to solution
07-28-2014 09:20 AM
NaveenKatta
New Contributor III

I am trying below code to access the layers, which is token based rest service with username and password..It is not working any idea???    

UserCredentials creds = new UserCredentials();

creds.setUserAccount("username", "password");

creds.setUserToken("huiqyuidhiqkljklwejk6Qzwgehwj6bqoKbjhwejkhiTBx", "localhost"); ArcGISDynamicMapServiceLayer layer = new ArcGISDynamicMapServiceLayer("http://.........", creds);

Thanks

0 Kudos
1 Solution

Accepted Solutions
NaveenKatta
New Contributor III

Solved last week. Problem was token not created using same domain.

Solution:

String tokenUrl =url + "tokens/generateToken?username=" *****&password=*****";
URL uRl = new URL(tokenUrl);
HttpURLConnection con = (HttpURLConnection) uRl.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Client", "HTTP Referer");
con.setRequestProperty("HTTP referer", "******");
con.connect();

use this generated token as below:

uRl = new URL(url+"&token=*******");

con = (HttpURLConnection) uRl.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Client", "HTTP Referer");
con.setRequestProperty("HTTP referer", "*****");
con.connect();
wr = new OutputStreamWriter(con.getOutputStream());
wr.flush();

View solution in original post

0 Kudos
5 Replies
EliseAcheson1
Occasional Contributor

Have you tried not calling setUserToken? Normally setting the username and password in the credentials is all that's required, the API does the rest.

Is it a specific service giving you grief?

~Elise

0 Kudos
NaveenKatta
New Contributor III

Yes, We do have other services but not based on token, which are working fine. I have tried not calling setUserToken but no use..If i debug and check the variables I am seeing LayerStatus=UNINITIALIZED...

I am able to see the layers if i go directly in the browser after giving user name and password....

-Naveen

0 Kudos
NaveenKatta
New Contributor III

any help??

Do we need to do any kind of settings from ArcGIS side??

0 Kudos
EliseAcheson1
Occasional Contributor

It's hard to say without more info about the service, how you're obtaining the token, and perhaps some context around the code.

You could try debugging using a program like Fiddler to see the chat to and from the service?

For reference here's some ArcGIS Server (10.2) doc on tokens: About ArcGIS Tokens

~Elise

0 Kudos
NaveenKatta
New Contributor III

Solved last week. Problem was token not created using same domain.

Solution:

String tokenUrl =url + "tokens/generateToken?username=" *****&password=*****";
URL uRl = new URL(tokenUrl);
HttpURLConnection con = (HttpURLConnection) uRl.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Client", "HTTP Referer");
con.setRequestProperty("HTTP referer", "******");
con.connect();

use this generated token as below:

uRl = new URL(url+"&token=*******");

con = (HttpURLConnection) uRl.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Client", "HTTP Referer");
con.setRequestProperty("HTTP referer", "*****");
con.connect();
wr = new OutputStreamWriter(con.getOutputStream());
wr.flush();
0 Kudos