当前位置: 首页 > 面试题库 >

尝试使用Google Directory API和服务帐户身份验证时收到错误消息“未授权访问此资源/ api”

胡国兴
2023-03-14
问题内容

我确实很难尝试使用服务帐户身份验证来使用Google Directory API(Admin SDK)。

使用基于客户端的三足式OAuth可以正常工作(在此处进行测试-https: //developers.google.com/admin-
sdk/directory/v1/reference/members/insert
),但是将权限委派给我所使用的服务帐户存在问题使用。在Google
Apps管理下,我启用了使用API​​的功能,并按照说明将服务帐户添加到了允许的OAuth客户端列表中。

这是代码:

import httplib2
import sys

from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

credentials = SignedJwtAssertionCredentials(
    '<KEY>@developer.gserviceaccount.com',
    '<KEY DATA>',
    scope='https://www.googleapis.com/auth/apps.groups.settings https://www.googleapis.com/auth/admin.directory.group https://www.googleapis.com/auth/admin.directory.group.member'
)
http = httplib2.Http()
http = credentials.authorize(http)

service = build("admin", "directory_v1", http=http)
groups = service.groups()
g = groups.get(groupKey="<GROUP NAME>").execute()

最终,出现以下错误:

apiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/groups/<GROUP NAME>?alt=json returned "Not Authorized to access this resource/api">

我也尝试使用以下API:

service = build("groupssettings", "v1", http=http)

但这也会返回一个错误-“后端错误”。


问题答案:

即使您使用的是服务帐户,您仍然需要在具有适当管理员权限的实例中代表Google Apps用户执行操作。尝试做:

credentials = SignedJwtAssertionCredentials(
  '<KEY>@developer.gserviceaccount.com',
  '<KEY DATA>',
  scope='https://www.googleapis.com/auth/apps.groups.settings https://www.googleapis.com/auth/admin.directory.group https://www.googleapis.com/auth/admin.directory.group.member',
  sub='super-admin@yourdomain.com'
)

其中super-admin@yourdomain.com是您Google Apps帐户中的超级管理员。



 类似资料:
  • 我试图在我的API中向一个endpoint发出请求,然而,每次试图访问执行器endpoint都会返回401错误,访问这个资源需要完全身份验证。我只是使用spring security对用户的密码进行加密,然后再将它们存储到数据库中。我已经检查了所有类似的问题,并执行了他们给出的所有解决方案,但仍然没有运气。 securityconfig.java userService.java

  • 我正在应用程序中实现和进行身份验证。 我有3个角色:管理员,版主和用户。 例如,在使用user role登录之后,我得到了主页,但是当我通过点击一个按钮访问用户空间时,我得到了: 2020-09-04 09:01:22.819错误10148---[nio-8080-exec-5]C.B.S.Security.jwt.AuthentryPointJwt:未经授权的错误:访问此资源需要完全身份验证 文

  • 我创建了一个SpringBoot/SpringSecurity/React web应用程序,在本地运行良好,但当我尝试在DigitalOcean生产服务器上运行它时,它失败了 未经授权的错误:访问此资源需要完全身份验证 我没有使用oauth2,只是Spring安全。主页不需要身份验证,所以我不明白错误。建议?

  • 例外消息:此应用未被授权使用firebase身份验证。请验证Firebase控制台中是否配置了正确的包名和SHA-1。[应用程序验证失败]。我已经将SHA-1和SHA-256键从游戏控制台添加到firebase项目设置中,但仍然不起作用。请求OTP时,弹出上述异常。请帮忙

  • 问题内容: 我正在尝试访问Googles Contacts API,但由于获得授权而失败。从其他(网络)语言开始,我习惯于使用API​​Console和公共API密钥(授权)。 这样,我不能够刷新令牌并不能确定如何使用公共密钥的accessToken ......相反,我尝试了一个服务帐户: 这是我的例外: 谢谢提示! 问题答案: 不调用我的代码就可以很好地工作。但是您只会拥有一个模拟帐户(ser