【发布时间】:2016-02-26 18:31:39
【问题描述】:
我只想从 Google API 返回的 JSON 中访问电子邮件地址和姓名。下面是我的代码的 sn-p。 jsonIdentity 返回整个 Json
字符串 USER_INFO_URL = "https://www.googleapis.com/oauth2/v1/userinfo";
public String getUserInfoJson(final String authCode) throws IOException {
final GoogleTokenResponse response = flow.newTokenRequest(authCode).setRedirectUri(REDIRECT_URI).execute();
final Credential credential = flow.createAndStoreCredential(response, null);
final HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(credential);
final GenericUrl url = new GenericUrl(USER_INFO_URL);
final HttpRequest request = requestFactory.buildGetRequest(url);
request.getHeaders().setContentType("application/json");
final String jsonIdentity = request.execute().parseAsString();
return jsonIdentity;
}
【问题讨论】:
-
以下链接定义了如何在 java 中读取 json。 examples.javacodegeeks.com/core-java/json/…
标签: java json jsp jakarta-ee oauth