Select to view content in your preferred language

Invalid Token- AGOL services

4275
7
Jump to solution
08-01-2013 03:59 AM
sumitzarkar
Deactivated User
I am having ArcGIS Online(AGOL) account and using Silverlight API - Identity Manager to generate the token to access the feature layer.
But this token is working some times and some time it wont.Feature layer initialization is failing due to invalid token.
The same case with the Routing service also i am not able to generate the routes using tokens. service is returning error INVALID TOKEN.
0 Kudos
1 Solution

Accepted Solutions
sumitzarkar
Deactivated User
Instead of this method i tried using OAUTH2 mechanism and it is working fine for all services.

Thank You!!!

View solution in original post

0 Kudos
7 Replies
DominiqueBroux
Esri Frequent Contributor
It might be because you generate tokens that are not associated to a referer.

Try by setting IdentityManager.Current.TokenGenerationReferer = <substring of your SL app URL>   (e.g "http".
0 Kudos
sumitzarkar
Deactivated User
It might be because you generate tokens that are not associated to a referer.

Try by setting IdentityManager.Current.TokenGenerationReferer = <substring of your SL app URL>   (e.g "http".


Thank You Dominique BROUX.

i tried setting that but still it is not working.
i am getting token but the layer is not getting initialized it is saying invalid token.
0 Kudos
RichardWatson
Deactivated User
Without the code the only thing that we can do is to guess what you might possibly be doing wrong.

Post the code.
0 Kudos
sumitzarkar
Deactivated User
Without the code the only thing that we can do is to guess what you might possibly be doing wrong.

Post the code.

HI Richard & Dominique,
Below is my code.
I have tried the same thing in WPF their also same problem.
If i set the referrer to "arcgis.com" it works for Routing service all the time however for feature layers, Layer gets initialized some times and some time it fails.

 private void login_Click(object sender, RoutedEventArgs e)
        {
            string url = "https://www.arcgis.com/sharing/generateToken";
            IdentityManager.Current.TokenGenerationReferer = <My IP Address which i got form http://Whatsmyip.com>;
            IdentityManager.Current.GenerateCredentialAsync(url, UserName.Text, Password.Password,
                (credential, ex) =>
                {
                    if (ex == null)
                    {
                       
                        AddFeatureLayers(credential.Token);
                    }

                });
        }
public void AddFeatureLayers(credential.Token){
 MyFeatureLayer = new FeatureLayer
            {
                ID = "TrucksLayer",
                Url = "<AGOL Feature Layer URL>",
                Token = Token              
            };
            MyFeatureLayer .OutFields.Add("*");
            MyFeatureLayer .Initialized += new EventHandler<EventArgs>(MyFeatureLayer_Initialized);
            MyFeatureLayer .InitializationFailed += new EventHandler<EventArgs>(MyFeatureLayer_InitializationFailed);
            _map.Layers.Add(MyFeatureLayer );
}

 void MyFeatureLayer_Initialized(object sender, EventArgs e)
        {
            Console.WriteLine("Layer  initialized");
        }

        void MyFeatureLayer_InitializationFailed(object sender, EventArgs e)
        {
            Console.WriteLine("Layer not initialized");
        }
0 Kudos
RichardWatson
Deactivated User
Suggest that you use Fiddler to inspect the call which is using the token that is failing.  Is the referer what you expect it to be?
0 Kudos
sumitzarkar
Deactivated User
I tried setting the referer to my global ip address but it is not working so i created one method to return me the token string.
This code is working properly for WPF application i am able to access the layer with the token which i got from this method, but when i am using this same method in Silverlight Application in some browsers at some time i am able to access the layer and some times not.
please let me know is this proper way to generate token for AGOL services, and also what should i do to work it in silverlight application.


 public static Task<string> GetToken(string userName, string password)
        {
            var tcs = new TaskCompletionSource<string>();
            string jsonData = "f=json&username=" + userName + "&password=" + password + "&referer=" + "http://arcgis.com" + "&request=getToken";
            WebClient getTokenClient = new WebClient();
            getTokenClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            getTokenClient.UploadStringCompleted += (sender, e) =>
            {
                JObject obj = JObject.Parse(e.Result.ToString());
                tcs.TrySetResult(obj["token"].ToString());
            };
            getTokenClient.UploadStringAsync(new Uri("https://www.arcgis.com/sharing/rest/generateToken"), "Post", jsonData);
            return tcs.Task;
        }

0 Kudos
sumitzarkar
Deactivated User
Instead of this method i tried using OAUTH2 mechanism and it is working fine for all services.

Thank You!!!
0 Kudos