【问题标题】:Google Admin Api gives HTTP error 403: Not Authorized to access this resource/apiGoogle Admin Api 给出 HTTP 错误 403: Not Authorized to access this resource/api
【发布时间】:2014-11-09 01:36:25
【问题描述】:

我正在尝试为 Directory API 创建一个自动化脚本来创建应用程序用户。我想我已经接近了,因为下面的代码在某种程度上运行:我可以输入“密码”参数,但之后与其他参数一起发送,它会因以下错误而崩溃:

引发 HttpError(resp, content, uri=self.uri) apiclient.errors.HttpError: https://www.googleapis.com/admin/directory/v1/users?alt=json 返回 "无权访问此资源/api">

这是脚本:

import httplib2
import pprint
import sys
import json
import urllib
import urllib2

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

def main(argv):
  # Load the key in PKCS 12 format that you downloaded from the Google API
  # Console when you created your Service account.
  f = file('key.p12', 'rb')
  key = f.read()
  f.close()

  # Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with the Credentials. Note that the first parameter, service_account_name,
  # is the Email address created for the Service account. It must be the email
  # address associated with the key that was created.
  credentials = SignedJwtAssertionCredentials('CLIENT_EMAIL', 
    key, scope='https://www.googleapis.com/auth/admin.directory.user')
  http = httplib2.Http()
  http = credentials.authorize(http)

  #Parameters for new user
  params = {'name':{'givenName':'John', 'familyName':'Smith'}, \
            'password':raw_input("Enter password "), 'primaryEmail':'jsfake@bn.co'}

  service = build("admin", "directory_v1", http=http)
  insertedUser = service.users().insert(body=params).execute()

if __name__ == '__main__':
  main(sys.argv)

【问题讨论】:

    标签: api directory admin


    【解决方案1】:

    似乎这个问题与这里的主题有关:Received error "Not Authorized to access this resource/api" when trying to use Google Directory API and Service Account Authentication

    您需要将具有管理员权限的帐户电子邮件传递到这样的凭据中:

    credentials = SignedJwtAssertionCredentials('CLIENT_EMAIL', 
        key, scope='https://www.googleapis.com/auth/admin.directory.user', sub='superadmin@domain.com')
    

    【讨论】:

      猜你喜欢
      • 2020-05-17
      • 1970-01-01
      • 2016-11-28
      • 2019-02-20
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多