java - Google API 身份验证问题
问题描述
我正在尝试运行以下示例
一切正常运行,直到第 7 步。我得到以下异常
com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "redirect_uri_mismatch",
"error_description" : "Bad Request"
}
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
at {package}.service.SecurityService.getProfile(SecurityService.java:55)
.....
我的代码如下所示:
public Result getProfile(User auth){
Result result = new Result();
try {
// Set path to the Web application client_secret_*.json file you downloaded from the
// Google API Console: https://console.developers.google.com/apis/credentials
// You can also find your Web application client ID and client secret from the
// console and specify them directly when you create the GoogleAuthorizationCodeTokenRequest
// object.
String CLIENT_SECRET_FILE = "client_secret.json";
GoogleClientSecrets clientSecrets = loadSecret(CLIENT_SECRET_FILE);
GoogleTokenResponse tokenResponse =
new GoogleAuthorizationCodeTokenRequest(
new NetHttpTransport(),
JacksonFactory.getDefaultInstance(),
"https://www.googleapis.com/oauth2/v4/token",
clientSecrets.getDetails().getClientId(),
clientSecrets.getDetails().getClientSecret(),
auth.getCode(),"http://localhost:8080/api/security/googleAPICallback")
.execute();
String accessToken = tokenResponse.getAccessToken();
// Use access token to call API
//GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
// Get profile info from ID token
GoogleIdToken idToken = tokenResponse.parseIdToken();
GoogleIdToken.Payload payload = idToken.getPayload();
auth.setAccessToken(accessToken);
auth.setUuid(payload.getSubject()); // Use this value as a key to identify a user.
auth.setEmail(payload.getEmail());
auth.setVerifiedEmail(payload.getEmailVerified());
auth.setName(String.valueOf(payload.get("name")));
auth.setPictureURL(String.valueOf(payload.get("picture")));
auth.setLocale(String.valueOf(payload.get("locale")));
auth.setFamilyName(String.valueOf(payload.get("family_name")));
auth.setGivenName(String.valueOf(payload.get("given_name")));
HashMap<String,Object> map = new HashMap<>();
Field[] fields = auth.getClass().getDeclaredFields();
for(Field field : fields){
field.setAccessible(true);
map.put(field.getName(), PropertyUtils.getSimpleProperty(field.getName(), field.getName()));
}
logger.info(auth.toString());
result.setCode(Result.OK);
result.setMessage("¡Exito!");
result.setVarious(false);
result.setData(map);
}catch (Exception e){
e.printStackTrace();
result.setCode(Result.BAD_REQUEST);
result.setMessage("¡No hay access_token!");
result.setVarious(false);
}
return result;
}
我已经尝试从本地生产服务器添加不同的端点。两个链接都接受 GET 和 POST 方法并返回“OK”json 响应。此外,这两个链接都已添加到 Google 控制台中的授权 URI 重定向表单中。
如果我留下一个空字符串,它会抛出并错误提示它需要一个 redirect_uri,并且我忽略了我抛出的那个空间并且错误提示它缺少我的令牌的方案。
额外的:
每次我在 Google 控制台中更改某些内容时,我都会重新下载我的 client-secret.json
解决方案
推荐阅读
- c++ - 无法设置条件变量
- sql - 我有一个从 SP 获取值的表。我想知道数据组合是否反向重复
- asp.net-mvc - IIS 8.5:为应用程序池“AppPool 名称”提供服务的进程无法响应 ping
- python - 将多个 json 文件转换为单个 csv 文件
- amazon-web-services - AWS 中的账户名和用户名有什么区别?
- php - Sonata Admin - 如何向字段添加链接
- python - Django runserver 问题“python manage.ph runserver”
- php - 为什么我的 PHP 表单不起作用?我确实收到了电子邮件,但里面没有内容
- c# - 短暂的事务缓存
- css - 如何单击外部按钮并保持活动样式?- AlpineJS & TailwindCSS