Woocommerce rest api OAuth authentication in android

What is the sample code for OAuth 1.0a authentication (single leg) in android? is there a library there ?, I'm using eclipse and I'm new to android. can someone clear the way for me?

+5
source share
2 answers

to answer my own question:

  • download the Scrib.jar library and add it to your lib folder (you can download it from ( here )
  • create a class called "OneLeggedApi10" and copy the code below in it:

    import org.scribe.builder.api.DefaultApi10a; import org.scribe.model.Verb; import org.scribe.model.Token; public class OneLeggedApi10 extends DefaultApi10a { @Override public String getAccessTokenEndpoint() { return null; } @Override public String getRequestTokenEndpoint() { return null; } @Override public String getAuthorizationUrl(Token requestToken) { return null; } @Override public Verb getAccessTokenVerb() { return Verb.GET; } @Override public Verb getRequestTokenVerb() { return Verb.GET; } } 


  • you can now perform OAuth authentication:

     String RESOURCE_URL = "http://yourDomain.com/wc-api/v3/orders"; String SCOPE = "*"; //all permissions Response response; OAuthRequest request; String responsebody = ""; OAuthService service = new ServiceBuilder().provider(OneLeggedApi10.class) .apiKey("your_key") .apiSecret("your_apiSecret") .signatureType(SignatureType.QueryString) .debug() /*.scope(SCOPE).*/ .build(); request = new OAuthRequest(Verb.GET, RESOURCE_URL); service.signRequest(new Token("", ""), request); // Now let go and ask for a protected resource! Log.d("scribe","Now we're going to access a protected resource..."); try{ response = request.send(); if (response.isSuccessful()) { responsebody = response.getBody(); } } catch (Exception e) { e.printStackTrace(); } 
  • note that if you are not using the above code in AsyncTask, put the request.send () part in the stream (in fact the entire try_catch section) to avoid running in the main thread exception.

  • finally, if you want to send data, for example, in the case when you want to update an order, replace

      request = new OAuthRequest(Verb.GET, RESOURCE_URL); 

    with these lines:

     String payload = yourJsonOBJ.toString(); request = new OAuthRequest(Verb.PUT, RESOURCE_URL); request.addHeader("Content-Type", "application/json"); request.addPayload(payload); 

For more information, see the WooCommerce Documentation Site
Hope this helps;)
good luck ..

+9
source
 new Thread() { @Override public void run() { String RESOURCE_URL = "http://www.woocommerce.com/wp-json/wc/v1/api/"; String SCOPE = "*"; //all permissions Response response; OAuthRequest request; String responsebody = ""; OAuthService service = new ServiceBuilder().provider(OneLeggedApi10.class) .apiKey("yourConsumerKey") .apiSecret("yourConsumerSecret") .signatureType(SignatureType.QueryString) .debug() /*.scope(SCOPE).*/ .build(); request = new OAuthRequest(Verb.GET, RESOURCE_URL); service.signRequest(new Token("", ""), request); // Now let go and ask for a protected resource! Log.d("scribe","Now we're going to access a protected resource..."); try { response = request.send(); if (response.isSuccessful()) { responsebody = response.getBody(); Log.v("response", responsebody); } } catch (Exception e) { e.printStackTrace(); } } }.start(); 

This code is updated from above, the above code works by getting JSON from the Wordpress Woocommerce API. But if you are interested in how to use Thread, this is the answer. And I add Log.v to get json response.

+1
source

All Articles