scottgrace

Registering Token Method not working

Discussion created by scottgrace on Oct 28, 2013
Latest reply on Jan 26, 2016 by brassier
I am trying to write my own widget to log-in to ArcGIS Online, and i am able to get the token back but i am never able to register the token with the identity manager so that  i can pull down all of the users' content to view. when i try to query the user i get back no results, when i look at the network activity in chrome i see that the search url does not have the token in it, so i tried manually copying that search link into a new tab and manually putting the token in the request URL and i was about to get my results back, but when i try to put the token string in the query parameters which does include the token string in the request URL it tells me that i have an invalid token...but if i right click the URL with the "invalid token" and open it in a new tab it works...im baffled and the methods on the documentation dont work....does anyone else have any experience or any idea on what to do?

<!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>

Outcomes