TransWikia.com

Create OAuth2 Authentication in pyqgis

Geographic Information Systems Asked by Pavel Pereverzev on January 19, 2021

I want to complete a code that should create same OAuth2 authentication on many other computers but with different usernames. I already tested creating basic authentication and it works fine.

am  = QgsApplication.authManager()
cfg = QgsAuthMethodConfig()
cfg.setId('test')
cfg.setName('test')
cfg.setMethod('Basic')
cfg.setConfig('username', "username")
cfg.setConfig('password', "password")
am.storeAuthenticationConfig(cfg)

But when I wanted to add OAuth2 authentication I stuck at adding bunch of parameters. Need to point Resource Owner grant flow and specify url, cliend id, client secret, username and password.

Like here:
enter image description here

Also tried to read configs of existing authentications to puth them into new configs but everytime got an empty configMap.

am  = QgsApplication.authManager()
for name, conf in am.availableAuthMethodConfigs().items():
    print(name) 
    print(conf.configMap())

Any thoughts?

One Answer

Found out how to perform it when user set OAuth2 connection manually.

This answer is a key. Just need to read existing authentications, grab their configMap() and put it into an empty config. Then add it to the authManager.

Reading all authentications' configs:

managerAU  = QgsApplication.authManager()
namesAU = [name for name in managerAU.availableAuthMethodConfigs().keys()]
for nameAU in namesAU:
    newAU = QgsAuthMethodConfig()
    managerAU.loadAuthenticationConfig(nameAU, newAU, True)
    cMap = newAU.configMap()
    print(cMap) # print grabbed config

Then when you have a config dict value you can use it in further authentication creations.

am  = QgsApplication.authManager()
cfg = QgsAuthMethodConfig()
cfg.setId('test')
cfg.setName('test')
cfg.setMethod('OAuth2')
cfg.setConfigMap(cMap)
am.storeAuthenticationConfig(cfg)

Answered by Pavel Pereverzev on January 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP