【发布时间】: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