2

I am using the below code to access a Magento api. I have used scribe to complete the Oauth process in which i am using fixed access token so I bypassed few steps of OAuth. Below program works fine when executing as java project but when I am trying it with android it always respond with error 500, service temporary unavailable. I have included CommonsCodec in both project to have similar encoding. during debug i am getting every thing same except response.

Please suggest what am I missing or, how alternate way to accomplish the same.

package org.scribe.examples;
import java.util.Scanner;

import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;

/**
 * Example test driver for the Magento17Api class - 
 * Magento Community 1.7.x and Enterprise 1.12 support REST APIs going forward
 * 
 * @see  http://www.magentocommerce.com/blog/the-magento-rest-api-a-better-way-to-integrate-business-applications/
 * @author Mike Salera
 */
public class MagentoExample
{
    public static final String MAGENTO_API_KEY = "MY key";
    public static final String MAGENTO_API_SECRET = "my secret";
    public static final String MAGENTO_REST_API_URL = "http://000.000.0.00/api/rest";

      public static void main(String[] args)
      {
        OAuthService service = new ServiceBuilder()
                                    .provider(MagentoAuth.class)
                                    .apiKey(MAGENTO_API_KEY)
                                    .apiSecret(MAGENTO_API_SECRET)
                                    .debug()
                                    .build();
        Scanner in = new Scanner(System.in);

        System.out.println("=== Mage v1.7.0.2 OAuth Workflow ===");
        System.out.println();

//      // Obtain the Request Token
//      System.out.println("Fetching the Request Token...");
//      Token requestToken = service.getRequestToken();
//      System.out.println("Got the Request Token!");
//      System.out.println();
//
//      System.out.println("Now go and authorize Scribe here:");
//      System.out.println(service.getAuthorizationUrl(requestToken));
//      System.out.println("And paste the verifier 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(requestToken, verifier);
        Token accessToken=new Token("hard coded access token", "hard coded secret");
        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, MAGENTO_REST_API_URL + "/customers");
        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.getBody());

        System.out.println();
        System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
      }
}

1 Answer 1

1

Scribe is not meant to be used in android as per my research on scribe current library. Finally i got it working by using oauth-signpost library it's working without any problem and its easy to integrate.

all you need to do is to include signpost-commonshttp4-1.2.1.1.jar and signpost-core-1.2.1.2.jar

 public OAuthConsumer getOauthConsumer() {
    OAuthConsumer consumer = new CommonsHttpOAuthConsumer(COUNSUMER_KEY, COUNSUMER_SECRET);
    consumer.setTokenWithSecret(OAUTH_TOKEN, OAUTH_SECRET);
    return consumer;
}

This will provide you OAuthConsumer that can be used to sign request

HttpPost request=new HttpPost(completeURL);
        getOauthConsumer().sign(request)

that's it you are done. Happy coding :)

Sign up to request clarification or add additional context in comments.

2 Comments

@RQube I am unable to get JSON from Magento REST API using above code. How to get that?
@OsamaMohammedShaikh I have moved to other project so can't share the code. Post your code in a new question and ping me url then i'll try to find a solution for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.