Getting 498: Invalid Token from AGS Server after performing Token Exchange

02-04-2016 02:35 PM
Regular Contributor

I'm trying to submit a very simply geocodeAddresses request to an ArcGIS for Server instance that is federated with Portal for ArcGIS. Getting the Portal Token and exchanging it for a Server token works fine and I can print the tokens and they are valid. However, when I submit the geocodeAddresses request to the Federated Server which hosts the Geocoding Service, the server responds with 498: Invalid Token. How could the token be invalid? The Portal just generated the token for the federated server!

import urllib

import urllib2

import httplib

import time

import json

import contextlib

def submit_request(request):

    """ Returns the response from an HTTP request in json format."""

    with contextlib.closing(urllib2.urlopen(request)) as response:

        job_info = json.load(response)

        return job_info

def get_PortalToken(username, password):

    """ Returns an authentication token for use in ArcGIS Online."""

    # Set the username and password parameters before

    #  getting the token.


    params = {"username": username,

              "password": password,

              "client": "requestip",

              "f": "json"}

    token_url = "{}/generateToken".format("")

    request = urllib2.Request(token_url, urllib.urlencode(params))

    token_response = submit_request(request)

    if "token" in token_response:

        print("Getting token...")

        token = token_response.get("token")

        return token


        if "error" in token_response:

            error_mess = token_response.get("error", {}).get("message")

            raise Exception("Portal error: {} ".format(error_mess))

def PortalToken_to_ServerToken(PortalToken):

    """Exchanges a Portal Token for a Server Token to provide access to restricted resources hosted on a Server federated with Portal"""

    params = {"token":PortalToken,

              "serverURL": "",


    tokenURL = "{}/generateToken".format("")

    request = urllib2.Request(tokenURL, urllib.urlencode(params))

    tokenResponse = submit_request(request)

    if "token" in tokenResponse:

        print("Exchanging token...")

        token = tokenResponse.get("token")


        return token


        if "error" in token_response:

            error_mess = token_response.get("error", {}).get("message")

            raise Exception("Portal error: {} ".format(error_mess))

def geocodeAddresses(token, addresses):

    """ Returns the result from a geocodeAddresses request"""

    params = {"token": token,

              "addresses": addresses,

              "f": "json"}

    geocode_url = "{}/geocodeAddresses".format("")

    request = urllib2.Request(geocode_url, urllib.urlencode(params))

    geocode_response = submit_request(request)

    if "error" in geocode_response:

        error_msg = geocode_response.get("error", {}).get("message")

        raise Exception("Error: {}".format(error_msg))


        return geocode_response


pToken = get_PortalToken("username", "password")

sToken = PortalToken_to_ServerToken(pToken)

addresses = '"records":[{"attributes":{"OBJECTID":1,"Address":"380 New York St.","City":"Redlands","Region":"CA","Postal":"92373"}},{"attributes":{"OBJECTID":2,"Address":"1 World Way","City":"Los Angeles","Region":"CA","Postal":"90045"}}]'

geocodeAddresses(sToken, addresses)

1 Reply
MVP Regular Contributor

Did you ever find the solution to this?  I am doing a similar thing (from C#).  I get the portal token, then use that to get the server token and get Invalid Token when I try to query the feature service.  Cannot figure out what is wrong

0 Kudos