[这正在Windows 10 64位计算机上运行,使用Python 3,Jupyter Labs版本1.6.1。
我正在使用Jupyter Notebook来访问我的本地门户网站的小脚本。通过下面的代码片段,您可以看到我正在寻求获取门户网站组以及每个组中的用户。
我正在获取的信息是每个用户的创建时间(不那么重要)以及上次访问的时间。这似乎很好用,该脚本会打印出用户及其帐户创建的日期,但最后一次访问日期的同一件事会引发错误。
我已审核arcgis.gis module来读取arcgis.gis.users类的参数,并且存在LastLogin。我还应该包括的另一点是,当我使用单个用户(例如我自己是登录用户)时,最后一个访问功能将起作用。似乎当我传递使用“ user2”变量循环的用户时,脚本不喜欢这样。
我还检查了.get()是否为可接受的参数,即“用户名”,这似乎很好。
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(如果不是)我们记录该用户尚未登录。
这里是脚本的更新代码。
帐户= gis.users.search()
对于帐户中的帐户:用户= 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(“处理完成”)