generateToken not working

2294
1
02-25-2016 11:20 AM
KyleDunaway
New Contributor III

I'm trying to generate a token to access

http://landscape1.arcgis.com/arcgis/rest/services/USA_Geology_Units/MapServer

var params = {

  username: username

  password: password

  //referer: 'http://l' + $(location).attr("host") + '/',

  referer: 'http://landscape1.arcgis.com/arcgis/rest',

  expiration: 1440,

  f: "json"

};

 

$.ajax({

    url: "https://www.arcgis.com/sharing/rest/generateToken?",

    type: "POST",

    dataType: "json",

    data: params,

    success: function (data) {

      the token in the data object is not working, the landscape1 just redirects to the login page.

Any ideas?

0 Kudos
1 Reply
EvelynHernandez
Occasional Contributor III

Here my function for that. I hope this helps.

function validate() {


var username =  'YOUR USERNAME';
var password = 'YOUR PASS';

    var http = new XMLHttpRequest();
    var url = "YOUR TOKEN URL";


    var str1 = "username=";
    var str2 = "&password=";
    var str3 = "&f=json&client=requestip&expiration=1440";
    var params = str1.concat(username, str2, password, str3);
    //f="json", client="requestip", expiration="1440"

    http.open("POST", url, true);


    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");


    http.onreadystatechange = function () {




        if (http.readyState == 4 && http.status == 200) {
            var tokenResponse = http.responseText;
            token=JSON.parse(tokenResponse);
            if (tokenResponse.search("error") == -1) {
             
                esri.id.registerToken( 
                {server:  'YOUR REST SERVICE', 
                  userId:  'YOUR EXAMPLE USER', 
                  token:  token.token, 
                  expires: token.expires, 
                  ssl:    false 
                }); 
               
               
                //DO SOMETHING
              .....
               
             
               
        } else {
                alert("Incorrect Login, try again.");
            }
        }
    };


    http.send(params);


    return false;




}
0 Kudos