如何解决AttributeError:Google API中的“资源"对象没有属性? [英] How do I resolve the AttributeError: 'Resource' object has no attribute in Google API?

查看:68
本文介绍了如何解决AttributeError:Google API中的“资源"对象没有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于整个上下文,我是脚本编写的新手.我正在尝试使用Google的API从Google Admin获取用户的完整列表,但我甚至在努力通过它的 build()部分.我标记到 service 上的任何内容都没有该属性,除了 close()

For full context I'm a newbie in scripting. I'm trying to get a full list of users from Google Admin using Google's API, but I'm struggling to even get passed the build() section of it. Whatever I tag onto service it just does not have that attribute, except for close()

我的最终目标是获得用户,用户别名和组名的完整列表,以便我可以在创建/分配它们之前检查它们是否已经存在.不幸的是,我陷入了这一障碍.

My end goal is to pretty much get a full list of users, user aliases, and group names so I can check if they're already existing before creating/assigning one. Unfortunately, I'm stuck at this hurdle.

我正在尝试使用的API: https://developers.google.com/admin-sdk/目录

API I'm trying to use: https://developers.google.com/admin-sdk/directory

代码:

 #!/usr/local/bin/env python3

import json
import os
import googleapiclient.discovery
from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/admin.directory.user.readonly',
'https://www.googleapis.com/auth/admin.directory.user.alias.readonly',
'https://www.googleapis.com/auth/admin.directory.group.readonly']
SERVICE_ACCOUNT_FILE = os.get.environ('SERVICE_ACCOUNT_FILE')

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

service = googleapiclient.discovery.build('admin', 'directory_v1', credentials=credentials)

如果只是尝试使用 service.execute()执行它,这就是我经常遇到的错误:

Here is the Error I keep running into if I were to just attempt to execute it with service.execute():

   Traceback (most recent call last):
      File "test.py", line 17, in <module>
        service.execute()
    AttributeError: 'Resource' object has no attribute 'execute'

如果我要在没有任何内容的情况下运行它,就在 service = ... 行之后,它不会吐出一个错误,因此我假设服务帐户文件是正确的./p>

If I were to run it without anything, just after the service = ... line, it does not spit out an error, so I'm assuming the service account file is correct.

推荐答案

service 变量,由此类可用于与您在构建服务时指定的API进行交互.根据这些参数, service 将具有可使用的特定API方法,但是 execute()不是其中之一.

This class can be used to interact with the API you specified when building the service. Depending on those parameters, the service will have specific API methods at its disposal, but execute() is not one of them.

由于您正在为 Directory API 构建服务,因此它们将是相应的与该API交互的方法:

Since you are building the service for Directory API, these will be the corresponding methods for interacting with this API:

例如,如果您要呼叫用户:列表为了列出您域中的所有用户,您需要执行以下操作:

For example, if you wanted to call Users: list in order to list all users in your domain, you would do the following:

service = googleapiclient.discovery.build('admin', 'directory_v1', credentials=getCreds())
response = service.users().list(domain="YOUR_DOMAIN").execute()

这篇关于如何解决AttributeError:Google API中的“资源"对象没有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆