AnsweredAssumed Answered

Generating Token By URL Request From ArcGIS Portal?

Question asked by mpiraesriaustralia-com-au-esridist Employee on Mar 26, 2020
Latest reply on Mar 26, 2020 by jskinner-esristaff

Hi,

I'm trying to generate a token from a Portal url. It generates a token when pasting this link into a web browser:

https://portalUrl/sharing/rest/generateToken?&f=json 

but when I run this code:

function getToken(portalUrl, username, password) {
  var xmlhttp = new XMLHttpRequest();
  var url = "https://portalUrl/sharing/rest/generateToken?username=username&password=password&f=json";


  xmlhttp.open("POST", url, false);
  xmlhttp.send();

  if (xmlhttp.status == 200) {
    var responseJSON = JSON.parse(xmlhttp.responseText)
    if (responseJSON && !responseJSON.error) {
      return responseJSON.token;     
    } else {
      return "ERROR";
    }  
  } else {
    return "ERROR"
  }
}

I get an error.

and I've created this in XHR code too:

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function() {
  if(this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "https://portalUrl/sharing/rest/generateToken?username=username&password=password&f=json");

xhr.send();

and this throws the following error:

 

{"error":{"code":400,"message":"Unable to generate token.","details":["POST request should not contain username and password in the query string."]}}

 

Any ideas or help is appreciated.

Thank you.

Outcomes