Google app engine 从Google应用程序引擎调用API
我试图从JSP中的GAE应用程序调用。应用程序已在AppSpot上运行。我想检索用户所属的所有组织单位。不幸的是,我在发出请求时得到了404代码,我不知道为什么Google app engine 从Google应用程序引擎调用API,google-app-engine,jsp,oauth,Google App Engine,Jsp,Oauth,我试图从JSP中的GAE应用程序调用。应用程序已在AppSpot上运行。我想检索用户所属的所有组织单位。不幸的是,我在发出请求时得到了404代码,我不知道为什么 ArrayList<String> scopes = new ArrayList<String>(); scopes.add("https://www.googleapis.com/auth/admin.directory.user"); AppIdentityService appIde
ArrayList<String> scopes = new ArrayList<String>();
scopes.add("https://www.googleapis.com/auth/admin.directory.user");
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes);
URL url = new URL("https://www.googleapis.com/admin/directory/v1/users/myuser@mygoogleappsdomain.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("Authorization", "OAuth " + accessToken.getAccessToken());
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
out.print("OK");
}
else {
out.print(connection.getResponseCode());
}
ArrayList scopes=new ArrayList();
作用域。添加(“https://www.googleapis.com/auth/admin.directory.user");
appIdentity服务appIdentity=appIdentity服务工厂。GetAppIdentity服务();
AppIdentityService.GetAccessTokenResult accessToken=appIdentity.getAccessToken(作用域);
URL=新URL(“https://www.googleapis.com/admin/directory/v1/users/myuser@mygoogleappsdomain.com);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setDoOutput(真);
connection.setRequestMethod(“POST”);
addRequestProperty(“内容类型”、“应用程序/json”);
addRequestProperty(“授权”、“OAuth”+accessToken.getAccessToken());
if(connection.getResponseCode()==HttpURLConnection.HTTP\u确定){
打印出来(“OK”);
}
否则{
out.print(connection.getResponseCode());
}
可以想象,这个代码片段打印404。基本上,我遵循的是GAE文档中提供的。我做错了什么?多谢各位
编辑:如果我只调用以下URL之一,我会得到403状态码。我的OAuth身份验证有什么问题吗
您的代码仅提供应用程序标识。您还需要获得用户的授权才能访问其目录信息 如果您按照提供的链接进行操作,则会到达以下状态:
因此,您需要通过OAuth 2身份验证+授权过程向用户发送消息,在该过程中,您将要求他们访问目录API。如果您只需要对用户列表进行只读访问,则需要请求
https://www.googleapis.com/auth/admin.directory.user.readonlySimult.不能看到代码有什么问题,但是你应该考虑使用它而不是自己做连接。这听起来很奇怪。我的意思是,我是域管理员,我不需要用户权限来访问他们的信息,因为我已经可以通过管理面板来访问了。也许我需要使用管理员帐户收集这些信息?你的应用程序需要获得你的批准才能访问目录API。好吧,帐户之间没有隐含链接:你是应用程序的管理员,然后使用应用程序不会通过你的帐户授予应用程序隐式访问所有API的权限。作为用户,您仍然需要执行OAuth过程,并为应用程序授予代表您访问API的显式权限。