Thanks to Pablo Fernandez for creating Scribe library for us.
First we have to generate the REST roles and consumers in the Magento dashboard, the roles are going to map the resources inside Magento that this role need access to and the consumer is going to represent the keys to be used inside our custom app that use the Scribe framework.
In order to add consumers to Magento from the REST api we create an OAuth REST Consumer where it will store the keys to be used by our app. We create them using the Magento dashboard and selecting System > Web Services > REST – OAuth Consumers like in the next image.
Let us first add dependency in the project:
1 compile 'org.scribe:scribe:1.3.7'
Android Rest API client will implemented as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
public class MainActivity extends AppCompatActivity { final String MAGENTO_API_KEY = "YOUR_MAGENTO_API_KEY"; final String MAGENTO_API_SECRET = "YOUR_MAGENTO_API_SECRET"; final String MAGENTO_REST_API_URL = "YOUR_MAGENTO_REST_API_URL"; private static Token requestToken; private OAuthService service; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // To override error of execution of network thread on the main thread StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); try { service = new ServiceBuilder() .provider(MagentoThreeLeggedOAuth.class) .apiKey(MAGENTO_API_KEY) .apiSecret(MAGENTO_API_SECRET) .debug() .build(); System.out.println("Magento'srkflow"); System.out.println(); // Obtain the Request Token System.out.println("FetchingRequest Token..."); requestToken = service.getRequestToken(); System.out.println("GotRequest Token!"); System.out.println(); System.out.println("FetchingAuthorization URL..."); String authorizationUrl = service.getAuthorizationUrl(requestToken); Log.d("DEBUG", authorizationUrl); System.out.println("GotAuthorization URL!"); System.out.println("Nownd authorize Main here:"); System.out.println(authorizationUrl); System.out.println("Ande the authorization code here"); System.out.print(">>"); String verifierCode =((EditText) findViewById(R.id.editText)).getText().toString(); Log.d("DEBUG", verifierCode); Verifier verifier = new Verifier(verifierCode); System.out.println(); System.out.println("TradingRequest Token for an Access Token..."); Token accessToken = service.getAccessToken(requestToken, verifier); System.out.println("GotAccess Token!"); System.out.println("(if curious it looks like this: " + accessToken + " )"); System.out.println(); OAuthRequest request = new OAuthRequest(Verb.GET, MAGENTO_REST_API_URL + "/products?limit=2"); service.signRequest(accessToken, request); Response response = request.send(); System.out.println(); System.out.println(response.getCode()); System.out.println(response.getBody()); System.out.println(); } catch (Exception e) { e.printStackTrace(); } } public static final class MagentoThreeLeggedOAuth extends DefaultApi10a { private static final String BASE_URL = "http://192.168.1.114/Mo3/"; @Override public String getRequestTokenEndpoint() { return BASE_URL + "oauth/initiate"; } @Override public String getAccessTokenEndpoint() { return BASE_URL + "oauth/token"; } @Override public String getAuthorizationUrl(Token requestToken) { return BASE_URL + "admin/oauth_authorize?oauth_token=" + requestToken.getToken(); //this implementation is for admin roles only... } } |
Congratulation now you can get data from the Magento Framework using Rest API in your Android Application…..