what is the sample code for OAuth 1.0a(one leg) authentication in android? is there a library for it? . I use eclipse and i'm new in android. can anyone clarify the path for me?
Asked
Active
Viewed 1,630 times
2 Answers
9
to answer my own question:
- download Scrib.jar library and add it to your lib folder(you can download it from (here)
create a class with name "OneLeggedApi10" and copy below code 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; } }
now you can do 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's 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 above code in an AsyncTask,then put the request.send() part in a thread (actually whole try_catch section) for avoiding run in main thread exception
finally if you want to send data,for example in a case that 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);
more information in WooCommerce Documentation site
Hope it help ;)
good luck..

Moradi
- 163
- 10
-
thank you, it is working. But response has a limit while assigning to that `String`. How can I get that in JSON format? – sabith.ak Feb 01 '18 at 10:09
-
in scribe library it only returns response in a single String.this way it supports a response with almost 2^31=2147483648 characters. check this page that shows Response class of scribe: http://grepcode.com/file/repo1.maven.org/maven2/org.scribe/scribe/1.3.0/org/scribe/model/Response.java#Response.0stream – Moradi Feb 16 '18 at 09:08
-
@Moradi gives an error Failed resolution of: Ljavax/xml/bind/DatatypeConverter; on this line service.signRequest(new Token("", ""), request);, How do I solve it ?? – Kaustubh Bhagwat Sep 25 '19 at 06:29
2
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's 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 is working getting JSON from wordpress Woocommerce API. But if you wondering how to use Thread this is the answer. And I add Log.v for see the json response.

Faisal
- 308
- 4
- 19
-
Failed resolution of: Ljavax/xml/bind/DatatypeConverter; on service.signRequest(new Token("", ""), request); , How do i solve it ?? – Kaustubh Bhagwat Sep 25 '19 at 06:33