如何使用自定义查询在Google管理sdk api中搜索用户



我正试图从我公司的一个域名中拉出暂停电子邮件帐户的用户。因为我们有不止一个域,我意识到我需要使用自定义查询来做到这一点。然而,如果你看看下面每当我运行我的代码,我给了一个"400坏请求"异常。有人能帮我一下吗?下面你可以看到我的代码:

    public void checkSuspended() throws IOException, NullPointerException, GeneralSecurityException { 
try { 
    Directory directory = GCAuthentication.getDirectoryService("xxxxx", "xxxxxx.p12", "xxxxxxx"); 
//  System.out.println("1"); 
    Directory.Users.List list = directory.users().list(); 
//  System.out.println("2");
    list.setQuery("GET https://www.googleapis.com/admin/directory/v1/users?domain=xxxxxxxx.ca&query=email:xxxxxxxxx");
//  System.out.println("3");
    com.google.api.services.admin.directory.model.Users user = list.execute(); 
//  System.out.println(user); 
    }
catch (NullPointerException e){ 
    e.printStackTrace(); 
}catch (IOException e){
    e.printStackTrace();
}catch (GeneralSecurityException e ){ 
    e.printStackTrace();
}

}

请注意,我的getDirectoryService方法不会在这里引起错误,因为我已经测试过了。

下面是我得到的例外:

    com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Bad Request",
    "reason" : "badRequest"
  } ],
  "message" : "Bad Request"
}
    at     com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.brookfieldres.library.test.ExtractionTest.checkSuspended(ExtractionTest.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

请告诉我哪里做错了。

谢谢!

400:错误的请求是由查询语句中的错误参数引起的:list.setQuery("GET https://www.googleapis.com/admin/directory/v1/users?domain=xxxxxxxx.ca&query=email:xxxxxxxxx");

从查询语句中删除'GET'

更多的注意:

查询参数必须为URL编码。例如,查询query=givenName:Jane*的URL编码为query=givenName%3AJane*。客户端库自动处理此URL编码。

这个线程可能也有帮助。

directory.users().get(userKey=email).execute()

相关内容

  • 没有找到相关文章

最新更新