Give access to the Hosted feature service using Token access

450
8
Jump to solution
02-21-2024 02:34 AM
AravinthkumarBalasubramanian
New Contributor III

Hello All, 

I have a Hosted feature service in the ArcGIS Enterprise, one of the client wanted to access the feature service using Rest services, so I have shared the feature service URL https://portal.de/server/rest/services/Hosted/point/FeatureServer

But when they wanted to use the layer in their system, it asks for a Token. So I have done some search and found this https://enterprise.arcgis.com/zh-cn/server/latest/administer/linux/enable-token-acquisition-through-... 

And I have enabled the token acquisiton, in the Server admin, But I don't understand this https://developers.arcgis.com/rest/users-groups-and-items/generate-token.htm

To generate a token and link to the feature service, as I am very new to this some detailed explanation will be very helpful. Thank you. 


 

0 Kudos
1 Solution

Accepted Solutions
HenryLindemann
Esri Contributor

Hi @AravinthkumarBalasubramanian, the token is a security mechanism that allows you to log into a service via an application, it is usually has a short life e.g. 2 hrs.' then you need a new token, if your client has a custom webpage, he needs to generate a token and attach it to the service. The token you have in the above code will not work because it is for use in portal, please see this sample on how to connect the service using java script you just need to Registe an Api key, instead of the token, but the tutorial will show you how to do that.


Add a feature layer | Overview | ArcGIS Maps SDK for JavaScript 4.28 | ArcGIS Developers

Regards

Henry

View solution in original post

8 Replies
HenryLindemann
Esri Contributor

Hi @AravinthkumarBalasubramanian, so it does depend on your workflow, the below code is how you do it in python, but if your client has ArcGIS Enterprise then, you can just setup a secured service,

Under content new item

HenryLindemann_0-1708513720277.png

 

all that you are trying to build is https://portal.de/server/rest/services/Hosted/point/FeatureServer/{layerid}?token={token}

 

import requests
import json
import logging


def get_token_portal(portal_admin_url, portal_username, portal_password, referer):
url = f"{portal_admin_url}/sharing/rest/generateToken"

payload = { 'f': 'json',
'username': portal_username,
'password': portal_password,
'client': 'referer',
'referer': referer,
'expiration': '100'}
headers = {
'content-type': "application/x-www-form-urlencoded",
'accept': "application/json",
'cache-control': "no-cache",
}

response = requests.request("POST", url, data=payload, headers=headers, verify=False)

return json.loads(response.text)['token']

def query_rest(url, token, other_parameters=None, type="POST"):
"""url Root URL with webadaptor"""
url = f"{url}"

payload = {
'f': 'json',
'token': token
}

headers = {
'content-type': "application/x-www-form-urlencoded",
'accept': "application/json",
'cache-control': "no-cache",
}
if other_parameters is None:
response = requests.request(type, url, data=payload, headers=headers, verify=False)
else:
response = requests.request(type, url, params=other_parameters, data=payload, headers=headers, verify=False)
if response.text == "":
return None
return json.loads(response.text)

if __name__ == "__main__":
portal_admin_url = 'https://e1091.esri-southafrica.com/portal'
username = 'portaladmin'
password = 'password'
server_url = "https://e1091.esri-southafrica.com/server/rest"
token_portal = get_token_portal(portal_admin_url, username, password, server_url)

req_export_backup = f"{server_url}/services/Hosted/service_8f8eccce65394272b49be5fb6f2e9087/FeatureServer/0"
response_version = query_rest(req_export_backup, token_portal, type="POST")
print(f"Result: {response_version}")
AravinthkumarBalasubramanian
New Contributor III

Hi @HenryLindemann

Thank you for the clear explanation, Yes, my client has ESRI Enterprise access, so should I create  a URL in the "New Item"  ? Kindly can you elaborate on that. Thank you.:-)

 

0 Kudos
HenryLindemann
Esri Contributor

Hi @AravinthkumarBalasubramanian , yes under new item select web, you should see this page,

HenryLindemann_0-1708516764825.png

then select store credentials this is so that your client does not log in every time he opens the service, you log in with a user account on your system that has access to the service. 

HenryLindemann_1-1708516863270.png

 

then just save

HenryLindemann_2-1708516889754.png

 

 

 

AravinthkumarBalasubramanian
New Contributor III

Hi @HenryLindemann

But this just creates another feature layer with the same data in it, but how do I share it like this  https://portal.de/server/rest/services/Hosted/point/FeatureServer/{layerid}?token={token}  I don't see the feature service in the ArcGIS rest service Directory. 

0 Kudos
HenryLindemann
Esri Contributor

@AravinthkumarBalasubramanian , can you give me the scenario of how you want to use the data?

The service will not display in the rest because it is coming from your server but you will be able to query it and use it in portal for ArcGIS.

Regards

Henry

0 Kudos
AravinthkumarBalasubramanian
New Contributor III

@HenryLindemann, Sure, we are hosting the feature service in the ArcGIS Enterprsise, our client wants to integrate the hosted feature service in their system, its a web based system, so they have asked for the Rest service,

so we have sent the https://portal.de/server/rest/services/Hosted/point/FeatureServer service. But they tried to use the service and integrate in the system they get an error "Token required". 

They have access to our enterprise system using credentials. The question is how do I create a token and attach to the Feature service so that they can access the service and integrate in their system.

We have Server Administrator for creating tokens, but I am not sure how this workflow goes.  

0 Kudos
AravinthkumarBalasubramanian
New Contributor III

So far I have used this script and generated a token and added to the Feature service like this example, 

https://portal.de/server/rest/services/Hosted/point/FeatureServer?token=affcx78d4c5xfc

But if I open the url it refreshs automatically multiple times. So How do I share the Hosted feature service with the token to the Client, kindly help me to understand the concept. 

from arcgis.gis import GIS

parameters ={
    "PortalURL": "",
    "PortalUserName": "",
    "PortalPassword": ""
}

for keys, values in parameters.items():
    url = parameters['PortalURL']
    username = parameters['PortalUserName']
    password = parameters['PortalPassword']
    
gis = GIS(url, username, password)

token = gis._con.token
print(f"Your ArcGIS token is: {token}")

 

0 Kudos
HenryLindemann
Esri Contributor

Hi @AravinthkumarBalasubramanian, the token is a security mechanism that allows you to log into a service via an application, it is usually has a short life e.g. 2 hrs.' then you need a new token, if your client has a custom webpage, he needs to generate a token and attach it to the service. The token you have in the above code will not work because it is for use in portal, please see this sample on how to connect the service using java script you just need to Registe an Api key, instead of the token, but the tutorial will show you how to do that.


Add a feature layer | Overview | ArcGIS Maps SDK for JavaScript 4.28 | ArcGIS Developers

Regards

Henry