google-cloud-platform - 使用服务帐户创建 Google API 用户
问题描述
我正在尝试使用 Googles Directory API 和服务帐户创建用户。但是我收到了错误
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://admin.googleapis.com/admin/directory/v1/users?alt=json returned "Not Authorized to access this resource/api". Details: "Not Authorized to access this resource/api">
我在 Google 控制台上创建了一个服务帐户并允许域范围的委派。它还说为我的项目启用了 Admin SDK API。但是我似乎无法创建用户。该文档使我有些困惑。这是我的实现
def create_googleuser(content, randpass):
''' This function creates a Google Apps account for a user passing webhook contents and password as arguments '''
# Get User info from Webhook and store them in variables
firstname = get_firstname(content)
secondname = get_secondname(content)
emailaddress = firstname + "." + secondname + "@example.com"
# Connect to google API
userscope = ['https://www.googleapis.com/auth/admin.directory.user']
service_account_credentials = ('serviceaccountcredentials.json')
credentials = service_account.Credentials.from_service_account_file(service_account_credentials, scopes=userscope)
userservice = googleapiclient.discovery.build('admin', 'directory_v1', credentials=credentials)
# Create a user dictionary with user details
userinfo = {"primaryEmail": emailaddress,"name":{"givenName":firstname,"familyName":secondname},"password":randpass}
print (emailaddress)
# Create user through googleAPI
userservice.users().insert(body = userinfo).execute()
我认为我的实现是错误的,而不是权限,因为 serviceaccountcredentials.json 应该具有正确的权限。有什么建议么?
解决方案
推荐阅读
- python - 需要帮助缩小 SciPy curve_fit 函数“随机”行为不端的原因
- java - JSONException String 无法转换为 JSONObject
- selenium-webdriver - QAF Perfecto - 无法使用 XML 中的测试数据
- html - iOS 在音频上传时使用“捕获”HTML 属性崩溃
- android - 程序类型已经存在:com.android.internal.http.multipart.ByteArrayPartSource
- angular - Angular:ngrx/效果的一些问题
- javascript - 如何调整 Jquery DataTable 中按钮的大小
- javascript - 如何在 GrdiView 中将第一列宽度设置为 Fixed
- angularjs - 组件upperCaseComponent的模板出错
- python - 没有google smtp,还有其他方法可以google登录吗?