Я начал работать с Яндекс API.
Это мой код:
@RequestMapping(value = "/successPayment", method = RequestMethod.GET)
@ResponseBody
public String testMethod(@RequestParam(value = "code", required = true) String code
) throws URISyntaxException, IOException {
//https://money.yandex.ru/oauth/authorize?client_id=id_app&response_type=code&redirect_uri=http://localhost:8080/successPayment&scope=payment-p2p
//Get Token
URL urlgetAccessToken = new URL(" https://money.yandex.ru/oauth/token");
Map<String,Object> paramsForToken = new LinkedHashMap<>();
paramsForToken.put("code",code);
paramsForToken.put("client_id", "id_app");
paramsForToken.put("grant_type", "authorization_code");
paramsForToken.put("redirect_uri", "http://localhost:8080/successPayment");
StringBuilder postDataForToken = new StringBuilder();
for (Map.Entry<String,Object> param : paramsForToken.entrySet()) {
if (postDataForToken.length() != 0) postDataForToken.append('&');
postDataForToken.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postDataForToken.append('=');
postDataForToken.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytesForToken = postDataForToken.toString().getBytes("UTF-8");
HttpURLConnection connForToken = (HttpURLConnection)urlgetAccessToken.openConnection();
connForToken.setRequestMethod("POST");
connForToken.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connForToken.setRequestProperty("Content-Length", String.valueOf(postDataBytesForToken.length));
connForToken.setDoOutput(true);
connForToken.getOutputStream().write(postDataBytesForToken);
Reader inForToken = new BufferedReader(new InputStreamReader(connForToken.getInputStream(), "UTF-8"));
StringBuilder sbForToken = new StringBuilder();
for (int cForToken; (cForToken = inForToken.read()) >= 0;)
sbForToken.append((char)cForToken);
String responseForToken = sbForToken.toString();
ObjectMapper mapperForToken = new ObjectMapper();
AccessToken JAccessToken = mapperForToken.readValue(responseForToken.toString(), AccessToken.class);
String token=JAccessToken.getAccess_token();
//request-payment
URL urlRequestPayment = new URL(" https://money.yandex.ru/api/request-payment");
Map<String,Object> paramsForRequestPayment = new LinkedHashMap<>();
paramsForToken.put("pattern_id","p2p");
paramsForToken.put("to", "4100115171799989");
paramsForToken.put("amount", "1.00");
paramsForToken.put("message", "perevod");
paramsForToken.put("comment", "perevod");
paramsForToken.put("test_payment", true);
paramsForToken.put("test_result", "success");
StringBuilder postDataRequestPayment = new StringBuilder();
for (Map.Entry<String,Object> param : paramsForRequestPayment.entrySet()) {
if (postDataRequestPayment.length() != 0) postDataRequestPayment.append('&');
postDataRequestPayment.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postDataRequestPayment.append('=');
postDataRequestPayment.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytesRequestPayment = postDataRequestPayment.toString().getBytes("UTF-8");
String bearer=Base64.getEncoder().encodeToString(token.getBytes(StandardCharsets.UTF_8)).replaceAll("(\n)", "");
HttpURLConnection connForRequestPayment = (HttpURLConnection)urlRequestPayment.openConnection();
connForRequestPayment.setRequestMethod("POST");
connForRequestPayment.setRequestProperty("Authorization:", "Bearer "+bearer);
connForRequestPayment.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connForRequestPayment.setRequestProperty("Content-Length", String.valueOf(postDataBytesRequestPayment.length));
connForRequestPayment.setDoOutput(true);
connForRequestPayment.getOutputStream().write(postDataBytesRequestPayment);
Reader inRequestPayment = new BufferedReader(new InputStreamReader(connForRequestPayment.getInputStream(), "UTF-8"));
StringBuilder sbForRequestPayment = new StringBuilder();
for (int cForRequestPayment; (cForRequestPayment = inRequestPayment.read()) >= 0;)
sbForRequestPayment.append((char)cForRequestPayment);
String responseForRequestPayment = sbForRequestPayment.toString();
/* ObjectMapper mapperForToken = new ObjectMapper();
AccessToken JAccessToken = mapperForToken.readValue(responseForRequestPayment.toString(), AccessToken.class);
String token=JAccessToken.getAccess_token();*/
return responseForRequestPayment;
}
Но у меня такая ошибка:
Это пример жетона авторизации, что Яндекс возвращенного меня:
4100115171799989.F304D864532B10B1B1BCF72C0BB2D7C07E651B145B8797CFCB372948F3FCD3B1576EFCB77C031597CBD226DC1F2421E09B5B63720B094B66FB566E4082F31922284E2EEF01B7EDFC388FA805E4C7D8B21734068F4A74A42F274F67E34CA47A8378EB9C5525002845060181BAF12BE9AB7414C34D3E91B159EFE61EB7124B5DF8
Пожалуйста, помогите мне решить эту проблему
.