根据您尝试使用的服务,只需选择合适的Google 客户端库并查看Google+ 示例。
所有 API 的前半部分应该基本相同。要获取用户信息,您需要该oauth2
库,然后执行以下操作(取自此示例):
// Set up the HTTP transport and JSON factory
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
// Set up OAuth 2.0 access of protected resources
// using the refresh and access tokens, automatically
// refreshing the access token when it expires
GoogleAccessProtectedResource requestInitializer =
new GoogleAccessProtectedResource(accessToken, httpTransport,
jsonFactory, clientId, clientSecret, refreshToken);
// set up global Oauth2 instance
Oauth2 oauth2 = new Oauth2.Builder(httpTransport, jsonFactory, requestInitializer)
.setApplicationName("Google-OAuth2Sample/1.0").build();
Userinfo userinfo = oauth2.userinfo().get().execute();