我已经能够使用谷歌管理API登录到谷歌应用程序并检索用户列表。我需要使用HTTPClient进行类似。我之前创建了一个服务帐户,并能够使用JWT方法获取访问令牌。 已使用管理控制台高级安全设置授予范围的授权权限。在请求中使用访问令牌来获取禁止的用户403
我需要使用此访问令牌来创建/更新/读取用户。尽管有授权请求给予服务帐户(多数民众赞成我怎么能得到令牌)我越来越禁止错误。
{
"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\": \"[email protected]\","
+"\"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;
}
是的,我已经从管理控制台通过超级管理员用户向给定客户端授予了授权(域范围访问)。只有这样做后,我的谷歌API的代码开始列出用户。对于这些范围,它早先给出了类似的授权错误“https://www.googleapis.com/auth/admin.directory.user","https://www.googleapis.com/auth/admin.directory.user.readonly “);.访问令牌来自于创建带有clientemail的JWT,用于服务帐户的私钥等等。想想也许该域需要用HTTPClient以某种方式设置 – user3240209
问题是使用谷歌信誉生成器工作创建的令牌,如果与常规HTTP GetMethod一起使用,但JWT生成的令牌说未经授权。如果使用tokeninfo url,则两者都有效 – user3240209
请通过编辑将您的代码置于原始问题中。 –