2

Current Setup:

I have a connection to our ArcGIS Enterprise Portal (on-prem) using the Add ArcGIS Server Layer connection in QGIS. I have created an Desktop Application Item in the portal and I can connect using OAuth in QGIS to access the feature layers. The layer I am accessing in QGIS is an hosted feature layer (from Survey123) with attachments. I would like to show the attachments in the layer popup when clicking on a feature in QGIS.

Problem:

The problem is that in QGIS the attachments are not sent per default via the REST Api when requesting the data of a feature.

How I would like to solve this:

  1. Access the token stored internally in QGIS after OAuth
  2. Use an HTML Widget in the Attribute Form and make a REST call to the feature service to request all attachments / images.
  3. Display the images within the HTML Widget

Currently I am stuck reading the token from the QgsApplication.authManager(). My code so far - unfortunately this only gives me the connection details but not the token:

authMgr = QgsApplication.authManager()
success, decrypted_config = authMgr.loadAuthenticationConfig(
    '791l69i',  # the id of the existing configuration to load (7 chars)
    QgsAuthMethodConfig(),  # a fresh new config object
    full=True,  # whether to decrypt the credentials
)

if success:
  print(decrypted_config.configMap())

1 Answer 1

0

I finally found a solution to my problem. I am using the openProject() macro in the Project Properties -> Macros section. It will query the self endpoint of the portal which describes the current authenticated user. Based on the OAuth2 it will authenticate myself. By doing so I will be able to read the Authorization header of the request which contains the token. This token is than saved as a project variable called project_esri_token and can be read within the HTML widget for further requests. Important to know is the my_authCfg property which is the ID of the authentication configuration which can be found in the top right corner of the OAuth2 authentication settings.

Macro:

from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
from PyQt5.QtCore import QUrl
from qgis.core import QgsApplication, QgsNetworkAccessManager, QgsProject

def openProject():
    auth_manager = QgsApplication.authManager()
    my_authCfg = '791l69i'
    network_manager = QgsNetworkAccessManager.instance()
    url = "https://<my-portal>/portal/sharing/rest/portals/self?f=json"
    request = QNetworkRequest(QUrl(url))
    reply = network_manager.blockingGet(request, my_authCfg)
    auth_header = request.rawHeader(b"Authorization").data().decode('utf-8')
    token = auth_header.replace("Bearer ", "")
    QgsProject.instance().setCustomVariables({"project_esri_token": token})    
    pass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.