You are here: Using REST > v2006.1 services > Tutorials and samples > Obtaining a REST v2006.1 token

S A M P L E S

Obtaining a REST v2006.1 token

To start using REST with ArcWeb Services, you need an authentication token to verify you are authorized to access ArcWeb Services. There are two ways to request an authentication token—using getToken or using getUserID. See Authentication for the differences between UserIDs and tokens.

WARNING: If a token from a getUserID request is hacked and hijacked, it can be used to consume your ArcWeb credits. This can be stopped by changing your password, which changes your UserID, invalidating the previous one, or you can disable the UserID feature on the ArcWeb UserID account page.

There are several ways to obtain a REST authentication token:

NOTE:  See Samples—terms and conditions for usage permissions.

Using C# .NET to obtain a REST token

This sample demonstrates how to obtain a REST token for ArcWeb Services using C# .NET. The sample includes code for obtaining a REST token and a REST UserID.

How to obtain a REST token using C#.NET

  1. Download the sample code (1c_token.zip) and unzip it to view the WebForm1_aspx_cs.htm file.
  2. Open Visual Studio 2003 and create a new Visual Studio C# project of type ASP.NET Web application called GetRestTokenAndUserID.
  3. Open WebForm1_aspx_cs.htm in a Web browser and copy the text into the C# WebForm of this project.
  4. Replace [ArcWeb username] and [ArcWeb password] in the code with your ArcWeb Services user name and password.
  5. NOTE: Before running this application, make sure the UserID feature is enabled on the ArcWeb UserID account page.

  6. Run the application by clicking the Run button in Visual Studio IDE.
  7. NOTE:  If you run this application locally, make sure the URL contains the machine name instead of the local host. This is required to generate the ArcWeb token containing the IP address of the requesting client, your machine. For example:

    http://<hostmachine>:<port>/<application context path>/<C# page name>.aspx

  8. A token and UserID are generated for your ArcWeb Services user name and password.

Code overview

The following lines in WebForm1_aspx_cs.htm are used in a request for a REST token to obtain a token. Replace [ArcWeb username] and [ArcWeb password] with your ArcWeb Services user name and password:

    string username="[ArcWeb username]"; //ArcWeb user name

    string password="[ArcWeb password]"; //ArcWeb password

Use the following lines to get the client IP address. This IP address is used for creating the token so that the token can be used for additional map requests originating only from this IP address. If this token is used from a different IP address, then an invalid token error message will be received:

    string ipAdd = "";//ip address of the requesting client

    ipAdd = Request.UserHostAddress; //ip of the client sending request

The following line creates an ArcWeb REST URL to generate an ArcWeb REST token:

    string urlToGetToken = "https://www.arcwebservices.com/services/v2006_1/restmap?actn=getToken&usr="+ username + "&pswd=" + password + "&ip=" +ipAdd;

The following code reads the XML response for an ArcWeb REST token:

    XmlTextReader reader = new XmlTextReader(urlToGetToken);

    while ( reader.Read() )

    {

    reader.MoveToContent();

    if (reader.Value != "")

    {

    token = reader.Value;

    }

    }

The token variable contains the generated token at the end of the while loop.

Using Java to obtain a REST token

This sample demonstrates how to obtain a REST token for ArcWeb Services using Java. When a valid HTTP GET or HTTP POST ArcWeb REST request is sent for an ArcWeb UserID or token, the ArcWeb server generates the UserID or token using the credentials supplied in the request in the XML format. This sample shows how to send the request to the ArcWeb server and parse the XML response containing the requested token.

Software requirements

How to obtain a REST token using Java

  1. Download the sample code (1getTokenAndUserID.zip) and unzip it to view the getTokenAndUserID.jsp file.
  2. Open getTokenAndUserID.jsp in a text editor and replace ArcWeb username and ArcWeb password in the getTokenAndUserID.jsp file with your ArcWeb Services user name and password:
  3. username: ArcWeb Username

    password: ArcWeb Password

  4. Save the file.
  5. NOTE:  Before running this application, make sure the UserID feature is enabled on the ArcWeb UserID account page.

  6. Place getTokenAndUserID.jsp in your Web server where it is accessible via HTTP/HTTPS.
  7. Make a request to cmrHTTPHeader.jsp by typing the following in a Web browser:
  8. http://<host>:<port>/<path to jsp>/cmrHTTPHeader.jsp

    NOTE: If you run this application locally, make sure the URL contains the machine name instead of the local host. This is required to generate the ArcWeb token containing the IP address of the requesting client, which is used to generate the token so that this token is valid for all subsequent map requests that originate from this requesting client. An Invalid Token error will be thrown if this token is used from a different client machine. Note that this is not the case for ArcWeb UserID.

  9. An HTML page displays with an ArcWeb Token and ArcWeb UserID.

Code overview

In the following lines in getTokenAndUserID.jsp, replace [ArcWeb username] and [ArcWeb password] with your ArcWeb Services user name and password:

The following line gets the IP address of the client:

The following code creates a URL to send a request to ArcWeb REST services to generate a token. Perform URL encoding to prevent special characters from breaking the code.

The following code opens a URL connection and prevents cached data from being used:

Create an instance of variable to store the XML response. Set properties to prevent XML validation and namespace usage, and ignore white spaces and comments.

The following code reads the XML response and parses it. An exception occurs when there is an error.

The following code gets the content of the <TOKEN> tag from the XML response. If the response does not contain a <TOKEN> tag, then it generates an error.

Using ProxyServlet to obtain a REST token

This sample demonstrates how to obtain a REST token for ArcWeb Services using the ProxyServlet method.

The ProxyServlet method:

In this method, the client makes the REST request to the ProxyServlet without using the tkn parameter. The ProxyServlet generates the token transparently to the end user, appends it to the parameters received from client, and makes a call to the ArcWeb Server. The ProxyServlet collects the response and passes the request to the client.

Software requirements

How to obtain a REST token using the ProxyServlet method using Apache with Tomcat

The following steps are for Apache with Tomcat Web server in the Windows operating environment:

  1. Make sure JDK and Tomcat are configured.
  2. Download 1restproxyservlet.zip and unzip it to the <TOMCAT_HOME>\webapps folder. When the file is unzipped, the directory structure should look like the following:
  3. <TOMCAT_HOME>
    >webapps
    >restproxyservlet
    >META-INF
    >context.xml
    >MANIFEST.MF
    >WEB-INF
    >web.xml
    >Classes
    >com
    >esri
    >web
    >proxy
    >restProxy.java
    >lib
    >commons-logging-1.0.4.jar
    >commons-io-1.1.jar

  4. Open the restProxy.java file in a text editor, and replace [username] and [password] with your ArcWeb Services user name and password. Save the file.
  5. Add the following to the classpath replacing <TOMCAT_HOME> with the name of your machine:
  6. %CLASSPATH%;%<TOMCAT_HOME>%\webapps\restproxyservlet\WEB-INF\lib\commons-logging-1.0.4.jar;%<TOMCAT_HOME>%\webapps\restproxyservlet\WEB-INF\lib\commons-io-1.1.jar;

  7. Open a command prompt, change to the directory containing restProxy.java, and compile restProxy.java by executing the following command:

    javac restProxy.java
  8. For example:

    c:\<TOMCAT_HOME>\webapps\restproxyservlet\WEB-INF\classes\com\esri\web\proxy>javac restProxy.java

  9. Restart your Web server.
  10. Make a REST request to invoke the restproxyservlet in a Web browser. For example:

    http://<localhost>:port/restproxyservlet/restProxy?actn=getMap&fmt=svg&sf=50000000&c=-117|34&ds=bam&fmt=svg
  11. This example displays a U.S. map in SVG format.


Visit the Feedback page to give comments or suggestions about the ArcWeb Developer's Guide.

ArcWeb site | ArcWeb support | support.esri.com

Copyright © ESRI