<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <script type="text/javascript"> var path_location = location.pathname.replace(/\/[^/]+$/, ''); var dojoConfig = { parseOnLoad: true, async : true }; </script> <script type="text/javascript" src="http://js.arcgis.com/3.7/"> </script> <script> var userLogin; require(["esri/IdentityManagerBase", "dojo/parser", "esri/kernel", "esri/urlUtils", "esri/arcgis/utils", "dojo/dom", "esri/IdentityManager", "esri/arcgis/Portal", "dojo/domReady!" ], function(IdentityManagerBase, parser, kernel, urlUtils, arcgisUtils, dom, IdentityManager, Portal ){ var _username; var _password; var tokenStr; var expireStr; userLogin = function() { _username = document.getElementById("user").value; _password = document.getElementById("pass").value; var request = esri.request({ url: "https://www.arcgis.com/sharing/rest/generateToken", content: { request: "generateToken", username: _username, password: _password, referer : "http://www.arcgis.com", f: "json" }, handleAs: "json", load: tokenObtained, error: tokenRequestFailed }, {usePost : true}); } function tokenObtained(response) { tokenStr = response.token; expireStr = response.expires; var token = { "server": "https://www.arcgis.com/sharing/rest", "userId": _username, "token": tokenStr, "ssl": true, "expires": expireStr }; esri.id.registerToken( {server: "https://www.arcgis.com/sharing/rest/", userId: _username, token: tokenStr, expires: expireStr, ssl: true }); //esri.id.registerToken(token); // kernel.id.registerToken(token); //console.log("token: ",token); displayItems(); //console.log("token: ",tokenStr); //"https://www.arcgis.com/sharing/rest/content/items/181dd71358454befae312bb01801b3ab/data?f=json&token="+tokenStr } function tokenRequestFailed(response) { console.log("in TORF", response); } function displayItems() { queryPortal(_username); } function queryPortal(portalUser) { var portal = new Portal.Portal("https://www.arcgis.com/"); //dojo.connect(portal, 'onLoad', loadPortal); //var user = portal.getPortalUser(); //See list of valid item types here: http://www.arcgis.com/apidocs/rest/index.html?itemtypes.html //See search reference here: http://www.arcgis.com/apidocs/rest/index.html?searchreference.html //function loadPortal() { var queryParams = { //token: tokenStr, q: "owner:" + portalUser, //+" AND (type: Feature Service OR type: Map Service)" , sortField: "numViews", sortOrder: "desc", num: 20 }; portal.queryItems(queryParams).then(createGallery); //} } function createGallery(items) { var htmlFragment = ""; arrayUtils.forEach(items.results, function(item) { htmlFragment += ( "<div class=\"esri-item-container\">" + ( item.thumbnailUrl ? "<div class=\"esri-image\" style=\"background-image:url(" + item.thumbnailUrl + ");\"></div>" : "<div class=\"esri-image esri-null-image\">Thumbnail not available</div>" ) + ( item.title ? "<div class=\"esri-title\">" + (item.title || "") + "</div>" : "<div class=\"esri-title esri-null-title\">Title not available</div>" ) + "</div>" ); }); dom.byId("itemGallery").innerHTML = htmlFragment; } }); </script> </head> <body> <h2>Sign-in to ArcGIS Online</h2> Username: <input name="username" id="user" size="27" type="text" /> <br /> Password: <input name="password" id="pass" size="27" type="password" /> <br /> <input type="button" name="action_login" value="Sign-in" onclick="userLogin()" /> </body> </html>
Do you have your proxy page all set up? I don't see any reference to it. I believe it's a requirement for anything related to the IdentityManager.
Did this issue ever get resolved? I'd like to do a similar thing (get a token out-of-band, and populate IdentityManager with it). I'm seeing the same result, where I can call registerToken(), and it seems to work, but interactions with the Portal objects never append the registered token.
FWIW, I was able to solve my problem and get the RegisterToken method to work. My issue was the referer parameter that was used when requesting a token. Make sure that maps back to the host that is serving the page.
In reference to the code above, I believe you'll need to call signIn on the Portal object before it will start appending tokens on requests. The IdentityManager and Portal objects seem to work together to pull that off:
new arcgisPortal.Portal("https://www.arcgis.com").signIn().then(function (portalUser) { //use portal here }
I am getting token when I pass client_id and client_secret.
I have passed the token generated to the kernel.id.registerToken(token) but when I try to invoke webmap from my server it is again propmting the default login dialog. Can anyone know the resolution for this issue without using proxy installation ?