Registering Token Method not working

3842
5
10-28-2013 01:04 PM
ScottGrace
New Contributor III
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>
0 Kudos
5 Replies
TracySchloss
Frequent Contributor
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.
0 Kudos
ScottGrace
New Contributor III
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.




I havent been using one, no. I have been able to register the a token using the appid and app secret using almost the same methodology before, and i just verified today that i didnt need the proxy for that.

Also i dont believe this sample has a poxy either.
https://developers.arcgis.com/en/javascript/jssamples/portal_oauth_inline.html
0 Kudos
BrianRassier
Occasional Contributor

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.

0 Kudos
BrianRassier
Occasional Contributor

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 }

venkat856
New Contributor

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 ?

0 Kudos