[英]How to get access token using gmail api - Java, spring Boot
嘗試使用 Spring Boot 和 Google api 實現OAuth2
。 我正在嘗試從 Google api 獲取訪問令牌。 這是我到目前為止的代碼:
應用.yml
gmail:
credentials:
secret: xxxxxxx
clientid: xxxxxxxx
auth-server-url: https://accounts.google.com/o/oauth2/token
scope: https://www.googleapis.com/auth/userinfo.profile
RegistrationServiceImpl.java
@Component
public class GmailServiceImpl implements GmailService {
private static final Logger log = LoggerFactory.getLogger(RegistrationController.class);
@Value("${gmail.credentials.secret}")
private String SECRETKEY;
@Value("${gmail.clientid}")
private String CLIENTID;
@Value("${gmail.auth-server-url}")
private String AUTHURL;
@Value("${gmail.scope}")
private String SCOPE;
private String redirect_uri = "http://localhost:8080/auth/realms/registerapirealm/broker/google/endpoint";
@Autowired
RestTemplate restTemplate;
@Override
public AuthCode getAuthCode() {
AuthCode responseToken = null;
try {
MultiValueMap<String, String> urlParameters = new LinkedMultiValueMap<>();
urlParameters.add("scope", SCOPE);
urlParameters.add("redirect_uri", redirect_uri);
urlParameters.add("response_type", "code");
urlParameters.add("access_type", "offline");
urlParameters.add("state", "state_parameter_passthrough_value");
urlParameters.add("client_id", CLIENTID);
responseToken = getGoogleAuthCode(urlParameters);
} catch (Exception e) {
e.printStackTrace();
}
return responseToken;
}
private AuthCode getGoogleAuthCode(MultiValueMap<String, String> urlParameters) {
AuthCode authCode = new AuthCode();
String uri = "https://accounts.google.com/o/oauth2/v2/auth";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(urlParameters, httpHeaders);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST, request, String.class);
log.info("{}", result);
log.info("{}", result.getBody());
String responseCode= result.getBody();
if (responseCode != null) {
authCode.setAuthCode(responseCode);
} else {
return null;
}
return authCode;
}
授權碼.java
@Data
public class AuthCode {
private String authCode;
}
我在 Postman 中收到下一個回復:
{
"authCode": "<!DOCTYPE html><head><title>Omdirigerar</title></head><body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#0000cc\" vlink=\"#551a8b\" alink=\"#ff0000\"><form jsname=\"RimvDb\" action=\"https://accounts.google.com/o/oauth2/v2/auth?trampoline=1&as=S-1442069211%3A1631459280968786\" method=\"POST\"><input type=\"hidden\" name=\"access_type\" value=\"offline\"><input type=\"hidden\" name=\"scope\" value=\"https://www.googleapis.com/auth/userinfo.profile\"><input type=\"hidden\" name=\"response_type\" value=\"code\"><input type=\"hidden\" name=\"redirect_uri\" value=\"http://localhost:8080/auth/realms/registerapirealm/broker/google/endpoint\"><input type=\"hidden\" name=\"state\" value=\"state_parameter_passthrough_value\"><input type=\"hidden\" name=\"client_id\" value=\"869675774281-68fa1n67en55nsov2hgbobmdm9sue00u.apps.googleusercontent.com\"></form><script nonce=\"kvN9vMtl0FIXHmh+Uxp+ww\">document.forms[0].submit();</script></body>"
}
我在這里錯過了什么?
顯然,您使用的是 grant_type authorization_code
,這意味着您必須在發出請求時提供代碼(授權代碼)。 基本上,應用程序將交換代碼以獲取訪問令牌。
code(必填) 該參數是客戶端之前從授權服務器收到的授權碼。
要開始授權流程,第一步是從授權服務器獲取代碼。 例子:
https://{authorization_server_endpoint}
?client_id=xxxxx
&redirect_uri={the url on which you want authorization server to redirect with the code}
&scope=xxxxx
&state=xxxxx
&response_type=code
您現在可以在以下調用中使用從上述步驟獲得的代碼與訪問令牌進行交換。
POST /oauth/token
Host: {authorization server}
grant_type=authorization_code
&code=xxxxxxxxxxx
&redirect_uri={the redirect url used in step one}
&client_id=xxxxxxxxxx
&client_secret=xxxxxxxxxx
對於谷歌授權服務器,您可以查看以下文檔。 https://developers.google.com/identity/protocols/oauth2/web-server#httprest
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.