【问题标题】:Using access token in request to fetch users forbidden 403在请求中使用访问令牌来获取用户禁止 403
【发布时间】:2014-01-31 09:50:20
【问题描述】:

我已经能够使用 google admin api 登录到 google 应用程序并检索用户列表。我需要使用 HTTPClient 做类似的事情。我之前创建了一个服务帐户,并且能够使用 JWT 方法获取访问令牌。 已使用管理控制台高级安全设置授予范围授权权限。

我需要使用此访问令牌来创建/更新/读取用户。尽管有对给定服务帐户的授权请求(这就是我获得令牌的方式),但我收到了禁止错误。

   {
    "domain": "global",
    "reason": "forbidden",
    "message": "Not Authorized to access this resource/api"
   }
  ],
  "code": 403,
  "message": "Not Authorized to access this resource/api"
 }
}

我已经检查了这个访问令牌

curl https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=#access_token

并查看它是有效的令牌。

示例 Java 代码段

     public void createUser()  {


        String params="{"
              +"\"name\": {"
              +"\"familyName\": \"Smith\","
              +"\"givenName\": \"John\","
              +"\"fullName\": \"John Smith\""
              +"},"
              +"\"password\": \"<some password>\","
              +"\"primaryEmail\": \"john.smith@xyz.net\","
              +"\"isAdmin\": false,"
              +"\"isDelegatedAdmin\": false,"
              +"\"isMailboxSetup\": true"
              +"}";


        PostMethod method =null;
        try {

            JSONObject json=new JSONObject(params);
            String url="https://www.googleapis.com/admin/directory/v1/users";
            //+ "?access_token="+ accessToken;
            method = new PostMethod(url);
            method.addRequestHeader("Content-Type", "application/json");
            method.addRequestHeader("Authorization","Bearer " + accessToken);

            method.setRequestEntity(new StringRequestEntity(json.toString(),
                    "application/json", null));
            method.execute();
            System.out.println(method.getResponseBodyAsString());
            if (method.getStatusCode() == HttpStatus.SC_CREATED) {
                try {
                    JSONObject response = new JSONObject(method.getResponseBodyAsString());
                    if (response.getBoolean("success")) {
                        System.out..println( "User Account created Successfully. <br>");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            method.releaseConnection;
        }

        return null;
    }

【问题讨论】:

    标签: google-admin-sdk


    【解决方案1】:

    这3个属性是只读的,创建用户时不应设置。

    +"\"isAdmin\": false,"
    +"\"isDelegatedAdmin\": false,"
    +"\"isMailboxSetup\": true"
    

    不过,这可能不是根本问题。错误消息表示权限问题,而不是身份验证问题。访问令牌从何而来?你在验证谁?如果它是服务帐户,则服务帐户需要拥有 Google Apps 实例的been granted domain-wide access 和拥有创建用户权限的be impersonating an admin in the Google Apps domain。如果访问令牌由普通 Google 帐户用户授权,则该用户必须是 Google Apps 域中的管理员或具有正确权限的经销商。如果 primaryEmail 属性中存在拼写错误,您会经常看到此错误。

    【讨论】:

    • 是的,我已经通过超级管理员用户从管理控制台向给定客户端授予了授权(域范围访问)。仅在这样做之后,我的 google api 代码就开始列出用户。对于这些范围“googleapis.com/auth/admin.directory.user","https://…); 之前它给出了类似的授权错误。访问令牌来自使用客户端电子邮件创建 JWT,服务帐户的私钥等。认为可能需要以某种方式使用 HTTPClient 设置域
    • 问题是使用谷歌凭据生成器创建的令牌如果与常规 HTTP GetMethod 一起使用,但 JWT 生成的令牌表示未经授权。如果与 tokeninfo url 一起使用,两者都显示有效
    • 已解决。问题是 JWT 创建的令牌在 Google API 工作时产生了问题。所以最终发现我们需要在声明集中指定prn字段来生成propert token。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2019-06-06
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2017-01-28
    相关资源
    最近更新 更多