【问题标题】:google admin directory v1 api groups.list insufficient permission errorgoogle admin directory v1 api groups.list 权限不足错误
【发布时间】:2013-07-18 13:09:11
【问题描述】:

我正在尝试使用 google admin api 进行授权并列出邮件列表用户。我从 api 控制台下载了一个密钥并做了:

require 'google/api_client'
client= Google::APIClient.new(application_name: "myapp", application_version: "0.1")
groups= client.discovered_api('admin', 'directory_v1')
key = Google::APIClient::PKCS12.load_key(Dir['*.p12'].first, 'notasecret')

client.authorization = Signet::OAuth2::Client.new(
  token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
  audience: 'https://accounts.google.com/o/oauth2/token',
  scope: 'https://www.googleapis.com/auth/admin.directory.group.readonly',
  issuer: '123asdf@developer.gserviceaccount.com',
  signing_key: key)
client.authorization.fetch_access_token!

puts client.execute(api_method: groups.users.list, parameters: {}).body

我尝试添加 groupKey:“mygroup@googlegroups.com” 我尝试设置域:“mysite.com” 总是导致“权限不足”

我还需要做什么才能列出组中的用户?

【问题讨论】:

    标签: ruby google-api google-admin-sdk google-groups-api google-api-ruby-client


    【解决方案1】:

    尝试类似:

    require 'google/api_client'
    
    ## Email of the Service Account #
    SERVICE_ACCOUNT_EMAIL = '<some-id>@developer.gserviceaccount.com'
    
    ## Email account of the Admin User ##
    ADMIN_EMAIL = 'your-google-admin@yourdomain.com'
    
    ## Path to the Service Account's Private Key file #
    SERVICE_ACCOUNT_PKCS12_FILE_PATH = '/path/to/<public_key_fingerprint>-privatekey.p12'
    
    ##
    # Build an Admin SDK client instance authorized with the service account
    # that acts on behalf of the given user.
    #
    # @param [String] user_email
    #   The email of the user.
    # @return [Google::APIClient]
    #   Client instance
    def build_client(user_email)
        key = Google::APIClient::PKCS12.load_key(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'notasecret')
        asserter = Google::APIClient::JWTAsserter.new(SERVICE_ACCOUNT_EMAIL,
            'https://www.googleapis.com/auth/admin.directory.group.readonly', key)
        client = Google::APIClient.new
        client.authorization = asserter.authorize(ADMIN_EMAIL)
        client
    end
    

    这大致改编自Google Drive Domain-Wide authorization 文档。将服务帐户与 Admin SDK Directory API 一起使用时,您仍需要模拟管理员用户。

    【讨论】:

    • 我将 ADMIN_EMAIL 设置为用于控制 api 控制台的电子邮件。结果是:``fetch_access_token':授权失败。服务器消息:(Signet::AuthorizationError) { "error" : "invalid_grant" }`
    • 我只启用了 Admin SDK 和组设置。 (这是免费层)
    • 这可能不适用于 Google Apps 标准/免费帐户。
    【解决方案2】:

    我也遇到过同样的问题。我写了一个示例要点,解释了如何设置它:

    https://gist.github.com/thomaswitt/7468182

    步骤是:

    1. 转到 Google 云端控制台 (https://cloud.google.com/console)
    2. 使用 P12 文件创建服务帐户
    3. 在 API 中启用 Admin SDK。
    4. 创建项目
    5. 在这个项目中创建一个注册的应用程序
    6. 转到“证书”部分并生成密钥
    7. 同时下载 JSON 文件
    8. 转到应用程序控制台 > 安全 > 扩展 > 3rdPartgy OAuth (https://admin.google.com/AdminHome?#OGX:ManageOauthClients)
    9. 添加 API 客户端。客户端名称是 JSON 中 client_id 的值 文件,API 范围为https://www.googleapis.com/auth/admin.directory.user.readonly

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-10
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多