Java Google端点:从本机客户端访问身份验证api

Java Google端点:从本机客户端访问身份验证api,java,google-app-engine,Java,Google App Engine,我尝试使用OAuth 2.0从本机应用程序访问google端点服务。我设法通过GoogleAuthorizationCodeFlow和JavaFXWebView(作为浏览器)进行身份验证。 在成功的身份验证之后,我尝试访问api方法,但是用户对象总是空的,问题是为什么 api方法调用的代码: GoogleAuthorizationCodeFlow flow = getGoogleAuthorizationCodeFlow(); Credential credential = flow.loadC

我尝试使用OAuth 2.0从本机应用程序访问google端点服务。我设法通过GoogleAuthorizationCodeFlow和JavaFXWebView(作为浏览器)进行身份验证。 在成功的身份验证之后,我尝试访问api方法,但是用户对象总是空的,问题是为什么

api方法调用的代码:

GoogleAuthorizationCodeFlow flow = getGoogleAuthorizationCodeFlow();
Credential credential = flow.loadCredential(USER_ID);
Helloworld.Builder builder = new Helloworld.Builder(new NetHttpTransport(), 
                                               new JacksonFactory(), credential);
Helloworld service = builder.build();
Helloworld.Greetings.Authed protectedApiMethod = service.
                                             greetings().authed();
HelloGreeting execute = protectedApiMethod.execute();
System.out.println("Response " + execute.getMessage());
用于创建流对象的代码:

private static GoogleAuthorizationCodeFlow getGoogleAuthorizationCodeFlow() {
    return new GoogleAuthorizationCodeFlow(new NetHttpTransport(), 
       new JacksonFactory(), INSTALLED_ID, CLIENT_SECRET, Arrays.asList(SCOPE_EMAIL));
}
我尝试进行身份验证的代码:

GoogleAuthorizationCodeFlow flow = getGoogleAuthorizationCodeFlow();
GoogleAuthorizationCodeTokenRequest tokenRequest = flow.newTokenRequest(code);
tokenRequest.setRedirectUri(REDIRECT_URL);
    try {
        GoogleTokenResponse execute = tokenRequest.execute();
        flow.createAndStoreCredential(execute, USER_ID);
        Platform.exit();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Api方法声明:

@ApiMethod(name = "greetings.authed",
           path = "greeting/authed",
          clientIds = {Constants.WEB_CLIENT_ID, Constants.INSTALLED_ID,     
              Constants.API_EXPLORER_CLIENT_ID})
public HelloGreeting authedGreeting(User user) {
    if (user != null) {
        HelloGreeting response = new HelloGreeting("hello " + user.getEmail());
        return response;
    } else {
        HelloGreeting response = new HelloGreeting("no user object was specified");
        return response;
    }
}
我得到的唯一响应是“未指定任何用户对象”。由于我可以毫无错误地调用该方法,我想我的身份验证是正确的。

来自文档:

如果来自客户端的请求具有有效的身份验证令牌或 在授权clientid列表中,后端框架提供 参数的有效用户。如果传入请求没有 有效的身份验证令牌,或者如果客户端不在ClientID白名单上, 框架将用户设置为null

因此,您必须手动捕获由基础结构提供的空用户的情况。所以要回答上述问题:请求无效。代码中的错误是,为实际请求重新创建了CodeFlow对象,但由于未设置CredentialStore,因此令牌丢失,无法重新发送