How to retrieve a private Web map using token?

7275
12
03-20-2012 08:49 AM
YukunXing
New Contributor III
Just started playing with the Silverlight API. I have been able to run the ESRI examples using a Web map hosted by ArcGIS and shared with everyone.
Then I set it to private and tried to retrieve it using token, but the map won't load.

The token was generated following these instructions:

A token is generated by entering the following in the address bar of your browser: https://www.arcgis.com/sharing/generateToken?f=json&request=gettoken&username=myusername&password=my..., where myusername and mypassword are replaced with your Esri Global Account username and password, respectively.

Caution:Make sure to use HTTPS instead of HTTP to generate the token.
This generates a response similar to the following:

{"token" :
"nEvfvFwGBaQ7laRKxUlH8ACGkwm2aHqUKiscQbyoG4lmVZU8EiG2NLa5nj6UKiEa","expires"
: 1306883380145}

Once you have the token, use its value to retrieve the JSON for the map by including "&token=" and the token value at the end of the URL. For example, http://www.arcgis.com/sharing/content/items/0a27f5cb1f07478fbdf117b70231c5c2/data?f=pjson&token=nEvf.... This uses the token value to request the formatted JSON from the item specified.


And this is part of my code:
....
           webMap.Token = "nEvfvFwGBaQ7laRKxUlH8ACGkwm2aHqUKiscQbyoG4lmVZU8EiG2NLa5nj6UKiEa";
            webMap.GetMapAsync("xxxxxx" );
...

In the event handler of GetMapCompleted, I checked the error message, it says "Invalid token".

What am I doing wrong here?

Thanks!
0 Kudos
12 Replies
DominiqueBroux
Esri Frequent Contributor
Might be a problem due to the referer (I am not sure which referer you set in your token request).

Instead of generating a token associated to a referer, you can try to generate a token associated to your ip address.

Something like:
https://www.arcgis.com/sharing/generateToken?f=json&username=myusername&password=mypassword&client=r...
0 Kudos
YukunXing
New Contributor III
Might be a problem due to the referer (I am not sure which referer you set in your token request).

Instead of generating a token associated to a referer, you can try to generate a token associated to your ip address.

Something like:
https://www.arcgis.com/sharing/generateToken?f=json&username=myusername&password=mypassword&client=r...


Thank you Dominique for your reply. I'm not sure what "referer" refers to, could you please elaborate a little?

I did try what you suggested (space added so the full string can be shown):
h t t ps://www.arcgis.com/sharing/generateToken?f=json&username=myusername&password=mypassword&client=requestip

while replacing "myusername", "mypassword", and "requestip" with the appropirate values, but got an effor message:

{"error":{"code":400,"message":"Unable to generate token.","details":["'referer' must be specified."]}}

Yukun
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I did try what you suggested (space added so the full string can be shown):
h t t ps://www.arcgis.com/sharing/generateToken?f=json&username=myusername&password=mypassword&client=requestip

but got an effor message:
{"error":{"code":400,"message":"Unable to generate token.","details":["'referer' must be specified."]}}


Strange! You got an error as if the option client=requestid was missing.

Do you get that error when clicking on this link : https://www.arcgis.com/sharing/generateToken?f=json&username=myusername&password=mypassword&client=r...
0 Kudos
YukunXing
New Contributor III
Strange! You got an error as if the option client=requestid was missing.

Do you get that error when clicking on this link : https://www.arcgis.com/sharing/generateToken?f=json&username=myusername&password=mypassword&client=r...


Yes:
{"error":{"code":400,"message":"Unable to generate token.","details":["Invalid username or password."]}}

And I should mention, I'm probably sitting behind a firewall, and internet connection is through a proxy server.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Yes:
{"error":{"code":400,"message":"Unable to generate token.","details":["Invalid username or password."]}}


It's no more the same message, the message you were talking about was : {"error":{"code":400,"message":"Unable to generate token.","details":["'referer' must be specified."]}}

Now my best bet is that your username or password is invalid 🙂
0 Kudos
YukunXing
New Contributor III
Dominique,

My username/password is valid. The problem was I changed "requestip" to my actual ip address. Once I left that alone and only replace my username/password, a token was generated. I was able to load the map in Silverlight using this token. Sorry for the confusion.

My assumption is that token generated this way is bound to the ip and won't work if the map request is sent from a different IP? Do you have any theory why the token generated using referer=MyToken won't work? If it works, it should be more flexible.

Thanks!

Yukun
0 Kudos
DominiqueBroux
Esri Frequent Contributor
The problem was I changed "requestip" to my actual ip address.

requestip is a keyword and has not to be replaced by something else : client=requestip means that the token is valid for the ip address that requests the token. You don't have to replace it by your ip address.
Sorry, I was not clear about that.

My assumption is that token generated this way is bound to the ip and won't work if the map request is sent from a different IP?

Right.

Do you have any theory why the token generated using referer=MyToken won't work?

referer is not a token but generally the web server url (i.e. http://.......).
The problem with Silverlight is that some browsers don't set correctly the HTTP referer. So generating a token associated to a referer may not work (except by using a proxy).

If you are using using IE, that should work.
For example, you can try to generate a token for the referer=http://<yourserver>
0 Kudos
YukunXing
New Contributor III

If you are using using IE, that should work.
For example, you can try to generate a token for the referer=http://<yourserver>


I am using IE. I have yet to find a server to deploy the example client to. Once I do I will give that a try.
Thank you again for your help Dominique!

Yukun
0 Kudos
SergioCharrua
New Contributor
Hello,

i have a similar problem too.
I'm using ArcGIS Portal SDK for Windows Phone , and testing on the WP Emulator.
The generateToken REST call does return a valid token. The POST request is as :

https://www.arcgis.com/sharing/rest/generateToken?&f=pjson&referer=<URL or IPAddress>&username=<username>&password=<password>&ssl=false

(please replace values in between <>)

The token is indeed returned :

{
  "token": "pf7FNl7aRCK9n4hgeL1XYp4gxIyWr0NQO0tVJChvOToUIOI80BGG2ROnp6y8zbv",
  "expires": 1352376303341,
  "ssl": false
}

But whenever i try to call another method passing the token, for example, using the following GET request:

https://www.arcgis.com/sharing/rest/community/groups?&f=pjson&q=<Group Name>&token=pf7FNl7aRCK9n4hgeL1XYp4gxIyWr0NQO0tVJChvOToUIOI80BGG2ROnp6y8zbv

I always get "Invalid token" error message, and cannot procede.

I'm using WP Emulator 7.1 and, please also note that yesterday morning this was working correctly and i did not change a single line of code in my project or did any update (to VS2010 , W7, WP7 Emulator, whatever....)

Also, if i execute both requests (generateToken and community/groups methods) directly on my desktop's browser (IE 9) this works fine....

I also tested the same code on a Silverlight project and another WPF project, and i get also the same error message...

This is a very strange behaviour...

Any clue?

Thanks in advance,

Sergio
0 Kudos