我与mercadolibre API进行交互,可以在这里找到它的java客户端库: https://github.com/mercadolibre/java-sdk
这些是关于验证的说明:
这是一个两步过程。
首先获取重定向用户的链接。这很容易!只是:
String redirectUrl = m.getAuthUrl("http://somecallbackurl");
这将为您提供重定向用户的网址。您需要指定一个回调网址,该网址将是用户在成功完成身份验证过程后重定向的网址。
一旦用户被重定向到您的回调网址,您将在查询字符串中收到一个名为code的参数。在这个过程的第二部分你需要这个。
m.authorize("the received code", "http://somecallbackurl");
这将为您的应用程序和您的用户提供accessToken和refreshToken(您的应用程序具有offline_access)。
在此阶段,您已准备好代表用户调用API。
当我执行m.authorize()
我收到以下错误:
GRAVE: El Servlet.service() para el servlet [jsp] en el contexto con ruta [/JAJA] lanzó la excepción [Ha sucedido una excepción al procesar la página JSP /index.jsp en línea 20
17:
18: String u=(String)request.getParameter("code");
19: if(u!=null){
20: m.authorize(u, "http://localhost:8082/JAJA");
21: Log.log(m.getAccessToken());
22: return;
23: }
Stacktrace:] con causa raíz
javax.net.ssl.SSLException: Unsupported record version Unknown-0.0
at sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)
at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:565)
at sun.security.ssl.InputRecord.read(InputRecord.java:529)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
at com.ning.http.client.providers.jdk.JDKAsyncHttpProvider$AsyncHttpUrlConnection.call(JDKAsyncHttpProvider.java:240)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
我正在使用JDK 8和tomcat 8
这是Meli Class,有一个洞java客户端:
package com.mercadolibre.sdk;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.concurrent.ExecutionException;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
import com.ning.http.client.FluentStringsMap;
import com.ning.http.client.Response;
public class Meli {
public static String apiUrl = "https://api.mercadolibre.com";
private String accessToken;
private String refreshToken;
private Long clientId;
private String clientSecret;
private AsyncHttpClient http;
{
AsyncHttpClientConfig cf = new AsyncHttpClientConfig.Builder()
.setUserAgent("MELI-JAVA-SDK-0.0.1").build();
http = new AsyncHttpClient(cf);
}
public Meli(Long clientId, String clientSecret) {
this.clientId = clientId;
this.clientSecret = clientSecret;
}
public Meli(Long clientId, String clientSecret, String accessToken) {
this.accessToken = accessToken;
this.clientId = clientId;
this.clientSecret = clientSecret;
}
public Meli(Long clientId, String clientSecret, String accessToken,
String refreshToken) {
this.accessToken = accessToken;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.refreshToken = refreshToken;
}
public String getAccessToken() {
return this.accessToken;
}
public String getRefreshToken() {
return this.refreshToken;
}
public Response get(String path) throws MeliException {
return get(path, new FluentStringsMap());
}
private BoundRequestBuilder prepareGet(String path, FluentStringsMap params) {
return http.prepareGet(apiUrl + path)
.addHeader("Accept", "application/json")
.setQueryParameters(params);
}
private BoundRequestBuilder prepareDelete(String path,
FluentStringsMap params) {
return http.prepareDelete(apiUrl + path)
.addHeader("Accept", "application/json")
.setQueryParameters(params);
}
private BoundRequestBuilder preparePost(String path,
FluentStringsMap params, String body) {
return http.preparePost(apiUrl + path)
.addHeader("Accept", "application/json")
.setQueryParameters(params)
.setHeader("Content-Type", "application/json").setBody(body)
.setBodyEncoding("UTF-8");
}
private BoundRequestBuilder preparePut(String path,
FluentStringsMap params, String body) {
return http.preparePut(apiUrl + path)
.addHeader("Accept", "application/json")
.setQueryParameters(params)
.setHeader("Content-Type", "application/json").setBody(body)
.setBodyEncoding("UTF-8");
}
private BoundRequestBuilder preparePost(String path, FluentStringsMap params) {
return http.preparePost(apiUrl + path)
.addHeader("Accept", "application/json")
.setQueryParameters(params);
}
public Response get(String path, FluentStringsMap params)
throws MeliException {
BoundRequestBuilder r = prepareGet(path, params);
Response response;
try {
response = r.execute().get();
} catch (Exception e) {
throw new MeliException(e);
}
if (params.containsKey("access_token") && this.hasRefreshToken()
&& response.getStatusCode() == 404) {
try {
refreshAccessToken();
} catch (AuthorizationFailure e1) {
return response;
}
params.replace("access_token", this.accessToken);
r = prepareGet(path, params);
try {
response = r.execute().get();
} catch (Exception e) {
throw new MeliException(e);
}
}
return response;
}
private void refreshAccessToken() throws AuthorizationFailure {
FluentStringsMap params = new FluentStringsMap();
params.add("grant_type", "refresh_token");
params.add("client_id", String.valueOf(this.clientId));
params.add("client_secret", this.clientSecret);
params.add("refresh_token", this.refreshToken);
BoundRequestBuilder req = preparePost("/oauth/token", params);
parseToken(req);
}
public String getAuthUrl(String callback) {
try {
return "https://auth.mercadolibre.com.ar/authorization?response_type=code&client_id="
+ clientId
+ "&redirect_uri="
+ URLEncoder.encode(callback, "UTF-8");
} catch (UnsupportedEncodingException e) {
return "https://auth.mercadolibre.com.ar/authorization?response_type=code&client_id="
+ clientId + "&redirect_uri=" + callback;
}
}
public void authorize(String code, String redirectUri)
throws AuthorizationFailure {
FluentStringsMap params = new FluentStringsMap();
params.add("grant_type", "authorization_code");
params.add("client_id", String.valueOf(clientId));
params.add("client_secret", clientSecret);
params.add("code", code);
params.add("redirect_uri", redirectUri);
BoundRequestBuilder r = preparePost("/oauth/token", params);
parseToken(r);
}
private void parseToken(BoundRequestBuilder r) throws AuthorizationFailure {
Response response = null;
String responseBody = "";
try {
response = r.execute().get();
responseBody = response.getResponseBody();
} catch (InterruptedException e) {
throw new AuthorizationFailure(e);
} catch (ExecutionException e) {
throw new AuthorizationFailure(e);
} catch (IOException e) {
throw new AuthorizationFailure(e);
}
JsonParser p = new JsonParser();
JsonObject object;
try {
object = p.parse(responseBody).getAsJsonObject();
} catch (JsonSyntaxException e) {
throw new AuthorizationFailure(responseBody);
}
if (response.getStatusCode() == 200) {
this.accessToken = object.get("access_token").getAsString();
JsonElement jsonElement = object.get("refresh_token");
this.refreshToken = jsonElement != null ? object.get(
"refresh_token").getAsString() : null;
} else {
throw new AuthorizationFailure(object.get("message").getAsString());
}
}
private boolean hasRefreshToken() {
return this.refreshToken != null && !this.refreshToken.isEmpty();
}
public Response post(String path, FluentStringsMap params, String body)
throws MeliException {
BoundRequestBuilder r = preparePost(path, params, body);
Response response;
try {
response = r.execute().get();
} catch (Exception e) {
throw new MeliException(e);
}
if (params.containsKey("access_token") && this.hasRefreshToken()
&& response.getStatusCode() == 404) {
try {
refreshAccessToken();
} catch (AuthorizationFailure e1) {
return response;
}
params.replace("access_token", this.accessToken);
r = preparePost(path, params, body);
try {
response = r.execute().get();
} catch (Exception e) {
throw new MeliException(e);
}
}
return response;
}
public Response put(String path, FluentStringsMap params, String body)
throws MeliException {
BoundRequestBuilder r = preparePut(path, params, body);
Response response;
try {
response = r.execute().get();
} catch (Exception e) {
throw new MeliException(e);
}
if (params.containsKey("access_token") && this.hasRefreshToken()
&& response.getStatusCode() == 404) {
try {
refreshAccessToken();
} catch (AuthorizationFailure e1) {
return response;
}
params.replace("access_token", this.accessToken);
r = preparePut(path, params, body);
try {
response = r.execute().get();
} catch (Exception e) {
throw new MeliException(e);
}
}
return response;
}
public Response delete(String path, FluentStringsMap params)
throws MeliException {
BoundRequestBuilder r = prepareDelete(path, params);
Response response;
try {
response = r.execute().get();
} catch (Exception e) {
throw new MeliException(e);
}
if (params.containsKey("access_token") && this.hasRefreshToken()
&& response.getStatusCode() == 404) {
try {
refreshAccessToken();
} catch (AuthorizationFailure e1) {
return response;
}
params.replace("access_token", this.accessToken);
r = prepareDelete(path, params);
try {
response = r.execute().get();
} catch (Exception e) {
throw new MeliException(e);
}
}
return response;
}
public BoundRequestBuilder head(String path) {
return null;
}
public BoundRequestBuilder options(String path) {
return null;
}
}