Back to project page Java-Yandex.Money-API-SDK.
The source code is released under:
MIT License
If you think the Android project Java-Yandex.Money-API-SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package ru.yandex.money.api; /* w w w .j a v a 2 s . c om*/ import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.message.BasicNameValuePair; import ru.yandex.money.api.response.ReceiveOAuthTokenResponse; import ru.yandex.money.api.rights.Permission; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collection; import java.util.List; /** * <p>??????? ???? ?????????? ?????? ???? API ???????.??????. ????????? ?????????? TokenRequester.</p> * <p>?? ???????????????? ?????? ?? ????????? ????????? ??????? Apache HttpClient. * ?? ?? ????????? ???????? ? ?????? BrowserCompatHostnameVerifier, * ????? ???????? ????????? ???????, ??? ????? ?????????? ??????? ????????????? * ???????? ???????.?????, ?? ??????? ?? ????????. ??? ????? ????????????? ????????? * ??????????? ???????.????? ?? ????? ????????, ??? ??? ? java ????????? ??????? * ?????????? ?????????????????? ??????????? CA, ????? ??? GTE CyberTrust * Solutions, Inc.</p> * * @author dvmelnikov */ public class TokenRequesterImpl implements TokenRequester { /** * ????????? ???? url encoding/decoding */ private static final String CHARSET = "UTF-8"; private final String clientId; private final YamoneyApiClient client; /** * ??????? ??????????? ????????. * * @param clientId ????????????? ??????????? ? ????????? ???????.?????? * @param client ???????????? HttpClient ???? ???????????????? ?? ????????? ???????.??????. * ???? request-payment ? process-payment ????? ??????????????? httpClient * c ????????? ?? 60 ??????? */ public TokenRequesterImpl(final String clientId, HttpClient client) { if (clientId == null || (clientId.equals(""))) { throw new IllegalArgumentException("client_id is empty"); } this.clientId = clientId; this.client = new YamoneyApiClient(client); } @Override public String authorizeUri(Collection<Permission> permissions, String redirectUri, Boolean mobileMode) { return authorizeUri(makeScope(permissions), redirectUri, mobileMode); } @Override public String authorizeUri(String scope, String redirectUri, Boolean mobileMode) { try { return (mobileMode ? URI_YM_AUTH_MOBILE : URI_YM_AUTH) + "?client_id=" + clientId + "&response_type=code" + "&scope=" + URLEncoder.encode(scope, CHARSET) + "&redirect_uri=" + URLEncoder.encode(redirectUri, CHARSET); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("unsupported encoding error", e); } } @Override public ReceiveOAuthTokenResponse receiveOAuthToken(String code, String redirectUri) throws IOException { List<NameValuePair> params = new ArrayList<NameValuePair>(); return receiveOAuthToken(code, redirectUri, params); } @Override public ReceiveOAuthTokenResponse receiveOAuthToken(String code, String redirectUri, String clientSecret) throws IOException { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("client_secret", clientSecret)); return receiveOAuthToken(code, redirectUri, params); } private ReceiveOAuthTokenResponse receiveOAuthToken(String code, String redirectUri, List<NameValuePair> params) throws IOException { params.add(new BasicNameValuePair("grant_type", "authorization_code")); params.add(new BasicNameValuePair("client_id", clientId)); params.add(new BasicNameValuePair("code", code)); params.add(new BasicNameValuePair("redirect_uri", redirectUri)); return client.executeForJsonObjectCommon(TokenRequester.URI_YM_TOKEN, params, ReceiveOAuthTokenResponse.class); } @Override public String getClientId() { return clientId; } @Override public String makeScope(Collection<Permission> permissions) { if (permissions == null) { throw new IllegalArgumentException("permissions expected"); } StringBuilder sBuilder = new StringBuilder(); for (Permission s : permissions) { sBuilder = sBuilder.append(" ").append(s.value()); } return sBuilder.toString().trim(); } }