Generate Token in REST

2683
8
Jump to solution
10-31-2018 08:24 AM
joerodmey
MVP Alum

Tryhing to generate an AGOL token using REST. WHat I have so far:

https://www.arcgis.com/sharing/generateToken HTTP/1.1?f=json&username=USERNAME&password=PASSWORD&client=requestip

Error I get:

{"error":{"code":400,"messageCode":"GWM_0002","message":"Invalid URL","details":[]}}
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Joe,

Here is an example on how to do this using python:

import requests, json

tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
response = requests.post(tokenURL, data = params, verify = False)
token = response.json()['token']

View solution in original post

8 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Joe,

Here is an example on how to do this using python:

import requests, json

tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
response = requests.post(tokenURL, data = params, verify = False)
token = response.json()['token']
joerodmey
MVP Alum

I don't have the option to use Python, I have to use REST directly. Reason being is that I'm using Integromat and webhooks

0 Kudos
joerodmey
MVP Alum
0 Kudos
JakeSkinner
Esri Esteemed Contributor

I'm not familiar with Integromat or webhooks, but I believe you need to create a POST request when generating a token.  For example, you could use Postman to do this.  Try this app and check to make sure your parameters are correct.

RandyBurton
MVP Alum

Have you tried adding it to the URL:

&token=yourtokenvalue
joerodmey
MVP Alum

Is there an option to receive the expiry time in minutes instead of milliseconds?

How can you remove the word token and expiry from the JSON response?

0 Kudos
joerodmey
MVP Alum

Can you format the JSON response to just have the token and no other text?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

In the python example I provided the response is a dictionary, so I can call the appropriate key ('token') to only receive that text.

token = response.json()['token']
0 Kudos