How to retrieve a private Web map using token?

7419
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
My best bet is that the referer you set is not the right one.

When sending web requests, WinPhone set a referer that looks like : file:///Applications/Install/<productId>/Install/, so when generating token you can try with the option referer=file:///Applications/Install/
0 Kudos
SergioCharrua
New Contributor
Merci Dominique,

it did work, which somehow it really is a odd or strange behaviour, as i said in my first post, the code worked liked a charm until yesterday morning (with referer=www.esri-portugal.pt) ...

Also, i had to create some HttpWebRequest helper class to make requests to the ArcGIS Portal services, instead of using the ArcGIS Portal SDK API.

For example , i cannot find a way to authenticate using the following statement:

            ArcGISPortal portal = new ArcGISPortal();
            string url = "http://www.arcgis.com/sharing/rest/";           
            portal.Token = token_from_previous_POST_request_to_generateToken_method;


            portal.InitializeAsync(url, (s, ex) =>
                {
                  
                   [Do something .....]

                }
            );

the portal object should have (i guess) the CurrentUser set, a Token etc.... and instead those values are always null.
If i make a POST request to generateToken to require a token, and set the portal instance Token property with the returned value, CurrentUser property is always null. Even tried with the following line:

            portal.Credentials = new System.Net.NetworkCredential(this.txtUsername.Text, this.txtPassword.Password);

but still no luck....

How can i use ArcGISPortal SDK on WP7 ?How to authenticate? Is there any online tutorials? I can't find anything relevant on ESRI's documentation...

Thanks once again,

Sergio
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Olà Sergio,

it did work, which somehow it really is a odd or strange behaviour, as i said in my first post, the code worked liked a charm until yesterday morning

Referer verification by portals is sometimes capricious but anyway using the right referer should help.

the portal object should have (i guess) the CurrentUser set, a Token etc.... and instead those values are always null.
If i make a POST request to generateToken to require a token, and set the portal instance Token property with the returned value, CurrentUser property is always null.


Strange! That should work as soon as you set the Portal token.

I just tried a WP program doing that. I get the right username:

using System.Windows;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Portal;
using Microsoft.Phone.Controls;

namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
            IdentityManager.Current.TokenGenerationReferer = "file:///Applications/Install/";
            IdentityManager.Current.GenerateCredentialAsync("http://www.arcgis.com/sharing/rest", "<username>", "<password>",
                (crd, err) =>
                {
                    if (crd != null)
                    {
                        var portal = new ArcGISPortal { Token = crd.Token };
                        portal.InitializeAsync(null, (p, e) => MessageBox.Show(p.CurrentUser.FullName));
                    }
                    else
                        MessageBox.Show("Error " + err.Message);
                });
        }
    }
}
0 Kudos