Python 3 arcgis.gis.user 用于访问 ArcGIS Portal 的 lastLogin

Python 3 arcgis.gis.user for lastLogin accessing ArcGIS Portal

这是 运行 在 Windows 10 64 位机器上,使用 Python 3,jupyter labs 版本 1.6.1。

我正在使用 Jupyter Notebook 编写一个小脚本来访问我的本地门户网站。通过下面的代码片段,您可以看到我正在寻找获取门户组和每个组中的用户。

我正在努力获取的信息是每个用户的创建时间(不那么重要)和上次访问的时间。这似乎工作得很好,该脚本打印出用户和他们的帐户创建日期,但最后访问日期的相同内容会引发错误。

我已经评论了 arcgis.gis module 阅读 arcgis.gis.users class 和 LastLogin 的参数确实存在。我应该包括的另一个注意事项是,当我使用个人用户时,例如我自己,如果我是登录用户,则最后访问功能有效。似乎当我传递使用 "user2" 变量循环的用户时,脚本不喜欢这样。

我还检查了 .get() 的可接受参数,即 "username",这看起来不错。

import time
from arcgis.gis import GIS
portal = r"*" #this would be the portal or arcgis online url
username = "*" #using admin credentials here
password = "*"
gis = GIS(portal, username, password)
groups = gis.groups.search()

for group in groups:
    print(group)
    accounts = gis.users.search()
    for account in accounts:
        #user = gis.users.account
        #user = gis.users.search(query="username = {}".format(account.username))
        #print(account.email)
        user2 = gis.users.get(username="{}".format(account.username))
        print(account.username)
        created_time = time.localtime(user2.created/1000)
        print("Created: {}/{}/{}".format(created_time[0], created_time[1], created_time[2]))
        last_accessed = time.localtime(user2.lastLogin/1000)
        print("Last active: {}/{}/{}".format(last_accessed[0], last_accessed[1], last_accessed[2]))

错误是我在第 23 行中为 last_accessed 变量传递 "user2" 时收到的错误。

 OSError                                   Traceback (most recent call last)
    <ipython-input-3-3a539a5c371d> in <module>
         21         created_time = time.localtime(user2.created/1000)
         22         print("Created: {}/{}/{}".format(created_time[0], created_time[1], created_time[2]))
    ---> 23         last_accessed = time.localtime(user2.lastLogin/1000)
         24         print("Last active: {}/{}/{}".format(last_accessed[0], last_accessed[1], 
    last_accessed[2]))
         25 

OSError: [Errno 22] Invalid argument

经过进一步调查,似乎 .lastLogin 片段有效。问题出在 ArcGIS 门户环境中,其中有一个用户尚未使用其凭据(通常使用管理员凭据)登录。传递到代码中的值传递了一个 -1 并且该值的转换没有在基础库中考虑,因此我们添加了一个 if 语句来验证该值是否需要 > 0,如果不是则我们记录用户尚未登录。

这是脚本的更新代码。

accounts = gis.users.search()

for account in accounts:
    user = gis.users.get("{}".format(account.username))

    if account.username.startswith("esri"): # Disregard any generic named accounts such as those that do not seem user specific like esri_boundaries, etc... 
        pass
    elif account.username.startswith("system_"): # Disregard any system related IDs such as "system_publisher" otherwise the parameter is not valid to pass for the last login.
        pass
    else:
        print("Full Name: {}".format(user.fullName))
        print("Username: {}".format(user.username))
        created = time.localtime(user.created/1000)
        print("User created: {}/{}/{}".format(created[0], created[1], created[2]))
        if user.lastLogin > 0:
            last_accessed = time.localtime(user.lastLogin/1000)
            print("Last active: {}/{}/{}".format(last_accessed[0], last_accessed[1], last_accessed[2]))
        else:
            print("***{} Has not yet logged in".format(account.username))
        print()
        
print("Process Complete")