Select to view content in your preferred language

Unable to Access feature service

1194
7
11-27-2023 03:32 AM
Labels (3)
NarendranathMandal
Emerging Contributor
class get_subtype(viewsets.ViewSet😞

    def list(self, request,projectname😞
       # Define your username and password
        username = ""
        password = ""

        # Build the URL for the feature service

        try:
            # Connect to the GIS using your username and password
            gis = GIS(url=feature_service_url, username=username, password=password)

            # Get information about the feature service
            feature_service = gis.content.get(feature_service_url)
            print("feature_service",feature_service)
            feature_service_info = feature_service.layers[0].properties  # Assuming you want information about the first layer
           
            print(feature_service_info)
           
            return Response(feature_service_info)

        except Exception as e:
            # Handle any errors here
            print(f"Error: {e}")
            return Response({"error": f"Error: {e}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
 
It throwing following error 
{"error":"Error: Access not allowed request\n(Error Code: 403)"}
Tags (1)
0 Kudos
7 Replies
EarlMedina
Esri Regular Contributor

There's a few problems going on. The url param when initializing the GIS should be the url to your portal. Additionally, your feature_service_url is not pointing to a Feature Service, but a Feature Service Layer. You should remove "/2"

0 Kudos
NarendranathMandal
Emerging Contributor

In my enterprise portal, there is a geodatabase file that I would like to access. From there, I would like to get the domain information. Could you please help me with this?

0 Kudos
Brian_Wilson
Honored Contributor

I try to break things up into smaller try:except: blocks so that the place the error happens is more apparent.

portal = "https://cyinprha.corp.cyient.com/portal/"
try:
  gis = GIS(url=portal, username=username, password=password)
  print("Logged in")
except Exception as e:
  print(e)
  return

I show connecting to Portal because I can't use "server" here. I assume that you don't have a federated portal, I've never worked in that environment.

I've never tried to use a hosted file gdb. Now I have to go try that!

Jupyter notebooks are a good way to test this stuff too. Especially from VS Code

 

0 Kudos
Brian_Wilson
Honored Contributor

Yeah the docs say url "should be a web address to either an ArcGIS Enterprise portal or to ArcGIS Online in the form: <scheme>://<fully_qualified_domain_name>/<web_adaptor>"

0 Kudos
NarendranathMandal
Emerging Contributor

I am able to connect to my portal for arcgis but unable  to access the feature service which is published as a gdb file 

0 Kudos
Brian_Wilson
Honored Contributor

Okay, well, I followed the instructions I found here: https://enterprise.arcgis.com/en/portal/latest/use/publish-features.htm#ESRI_SECTION1_F878B830119B44...

I put a feature class into an FGDB and then zipped it, then in Portal I published it as a feature service. My URL is similar to yours so I am assuming you did something similar. It's like publishing a shapefile.

My feature service is https://delta.co.clatsop.or.us/server/rest/services/Hosted/comm_gdb/FeatureServer

Now I have a way to try out your code.

Geez the docs are so hard to use now, but all nicely formatted in Esri web site! Sad. "Not invented here" syndrome. Anyway.

The contentmanager (https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#contentmanager) "get"method only accepts an item id from Portal and you are feeding it a URL. Find the item id and feed it that instead.

import pprint

# assuming you did the login thing here and have an object "gis"...

item = gis.content.search(service_url)[0]
pprint.pprint(item)

Not sure what you want to do from here. For my feature services I get this:

<Item title:"comm_gdb" type:Feature Layer Collection owner:bwilson@CLATSOP>

 

0 Kudos
Brian_Wilson
Honored Contributor

I try to answer questions here because I learn a LOT every time I do. This time around I learned how to publish a FGDB. I probably never will 🙂 but it's good to know how it works.

I never tried to do a "search" on a complete URL so it was interesting to see that it worked at all. Normally I search on a title or some short string. There are many options for the search method, arguably too many. 🙂

I don't see a quick way to get the itemId from the "item" object so that you can do the "get" call, and you can't feed the "item" into it.

I dug around in my own code and found how I do it. I bypass the Python API and use a _con. Looks like this

class PortalContent(object):
    def __init__(self, gis):
        self.gis = gis
        return

    def findItems(self, title=None, name=None, type=None) -> list:
        """ 
        Search the Portal using any combination of name, title, and type.
        Return the list of items, which might be empty.
        """        
        connection = gis._con

        # https://developers.arcgis.com/rest/users-groups-and-items/search-reference.htm
        url = connection.baseurl + 'search'
        q = ''
        if name:
            q += 'name:"%s"' % name
        if title:
            if q: q += ' AND '
            q += 'title:"%s"' % title
        if type:
            if q: q += ' AND '
            q += 'type:"%s"' % type
        params = {
            'q': '',     # This is required. This is the fuzzy match operation.
            'filter': q  # This is the exact match operation.
        }
        res = connection.post(url, params)
        return res['results']


    def findIds(self, title=None, name=None, type=None) -> list:
        """ 
        Search the Portal using any combination of name, title, and type.
        Return a list of ids, which might be empty.
        """
        items = self.findItems(title, name, type)
        ids = [item['id'] for item in items]
        return ids

 

0 Kudos