Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

MercadoLibre API Adapter #212

Closed
wants to merge 1 commit into from

2 participants

@pablomolnar

Hi Pablo Fernandez,

I've added the MercadoLibre OAuth 2.0 API Adapter.

I have to modify two core files to support Accept header. Feel free to make a suggestion/modification.

Cheers,
Pablo Molnar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 9, 2012
  1. @pablomolnar

    MercadoLibre API Adapter

    pablomolnar authored
This page is out of date. Refresh to see the latest.
View
4 .gitignore
@@ -9,7 +9,9 @@
.idea
scribe.iml
+scribe.ipr
+scribe.iws
# Binaries
-target
+target
View
22 src/main/java/org/scribe/builder/api/DefaultApi20.java
@@ -42,6 +42,28 @@ public Verb getAccessTokenVerb()
{
return Verb.GET;
}
+
+ /**
+ * Returns accept media type which can be used to specify certain media type which is acceptable for the response.
+ * Optional parameter.
+ *
+ * @return accept media type
+ */
+ public String getAcceptMediaType()
+ {
+ return null;
+ }
+
+
+ /**
+ * Returns true if accept media type is set
+ *
+ * @return true if accept media type is set
+ */
+ public boolean hasAcceptHeader() {
+ return getAcceptMediaType() != null;
+ }
+
/**
* Returns the URL that receives the access token requests.
View
49 src/main/java/org/scribe/builder/api/MercadoLibreApi.java
@@ -0,0 +1,49 @@
+package org.scribe.builder.api;
+
+import org.scribe.extractors.AccessTokenExtractor;
+import org.scribe.extractors.JsonTokenExtractor;
+import org.scribe.model.OAuthConfig;
+import org.scribe.model.Verb;
+import org.scribe.utils.Preconditions;
+
+import static org.scribe.utils.URLUtils.formURLEncode;
+
+/**
+ * @author: Pablo Molnar
+ */
+public class MercadoLibreApi extends DefaultApi20
+{
+ private static final String AUTHORIZE_URL = "https://auth.mercadolibre.com.ar/authorization?response_type=code&client_id=%s&redirect_uri=%s";
+ private static final String ACCESS_TOKEN_URL = "https://api.mercadolibre.com/oauth/token?grant_type=authorization_code";
+ private static final String ACCEPT_MEDIA_TYPE = "application/json";
+
+
+ @Override
+ public String getAccessTokenEndpoint()
+ {
+ return ACCESS_TOKEN_URL;
+ }
+
+ @Override
+ public String getAuthorizationUrl(OAuthConfig config)
+ {
+ Preconditions.checkValidUrl(config.getCallback(), "Must provide a valid url as callback. MercadoLibre does not support OOB");
+ return String.format(AUTHORIZE_URL, config.getApiKey(), formURLEncode(config.getCallback()));
+ }
+
+ @Override
+ public Verb getAccessTokenVerb() {
+ return Verb.POST;
+ }
+
+ @Override
+ public AccessTokenExtractor getAccessTokenExtractor()
+ {
+ return new JsonTokenExtractor();
+ }
+
+ @Override
+ public String getAcceptMediaType() {
+ return ACCEPT_MEDIA_TYPE;
+ }
+}
View
3  src/main/java/org/scribe/oauth/OAuth20ServiceImpl.java
@@ -33,6 +33,8 @@ public Token getAccessToken(Token requestToken, Verifier verifier)
request.addQuerystringParameter(OAuthConstants.CODE, verifier.getValue());
request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
if(config.hasScope()) request.addQuerystringParameter(OAuthConstants.SCOPE, config.getScope());
+ if(api.hasAcceptHeader()) request.addHeader("Accept", api.getAcceptMediaType());
+
Response response = request.send();
return api.getAccessTokenExtractor().extract(response.getBody());
}
@@ -58,6 +60,7 @@ public String getVersion()
*/
public void signRequest(Token accessToken, OAuthRequest request)
{
+ if(api.hasAcceptHeader()) request.addHeader("Accept", api.getAcceptMediaType());
request.addQuerystringParameter(OAuthConstants.ACCESS_TOKEN, accessToken.getToken());
}
View
67 src/test/java/org/scribe/examples/MercadoLibreExample.java
@@ -0,0 +1,67 @@
+package org.scribe.examples;
+
+import org.scribe.builder.ServiceBuilder;
+import org.scribe.builder.api.MercadoLibreApi;
+import org.scribe.model.*;
+import org.scribe.oauth.OAuthService;
+
+import java.util.Scanner;
+
+/**
+ * @author: Pablo Molnar
+ */
+public class MercadoLibreExample
+{
+ private static final String NETWORK_NAME = "MercadoLibre";
+ private static final String PROTECTED_RESOURCE_URL = "https://api.mercadolibre.com/users/me";
+ private static final Token EMPTY_TOKEN = null;
+
+ public static void main(String[] args)
+ {
+ // Replace these with your own api key and secret
+ String apiKey = "2731";
+ String apiSecret = "1TfBiXqXbRGKbHWcaknf5CpBXrLczTaJ";
+ OAuthService service = new ServiceBuilder()
+ .provider(MercadoLibreApi.class)
+ .apiKey(apiKey)
+ .apiSecret(apiSecret)
+ .callback("http://www.example.com/oauth_callback/")
+ .build();
+ Scanner in = new Scanner(System.in);
+
+ System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
+ System.out.println();
+
+ // Obtain the Authorization URL
+ System.out.println("Fetching the Authorization URL...");
+ String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
+ System.out.println("Got the Authorization URL!");
+ System.out.println("Now go and authorize Scribe here:");
+ System.out.println(authorizationUrl);
+ System.out.println("And paste the authorization code here");
+ System.out.print(">>");
+ Verifier verifier = new Verifier(in.nextLine());
+ System.out.println();
+
+ // Trade the Request Token and Verfier for the Access Token
+ System.out.println("Trading the Request Token for an Access Token...");
+ Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
+ System.out.println("Got the Access Token!");
+ System.out.println("(if your curious it looks like this: " + accessToken + " )");
+ System.out.println();
+
+ // Now let's go and ask for a protected resource!
+ System.out.println("Now we're going to access a protected resource...");
+ OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
+ service.signRequest(accessToken, request);
+ Response response = request.send();
+ System.out.println("Got it! Lets see what we found...");
+ System.out.println();
+ System.out.println(response.getCode());
+ System.out.println(response.getBody());
+
+ System.out.println();
+ System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
+
+ }
+}
Something went wrong with that request. Please try again.