android中的woocommerce rest api OAuth身份验证

问题描述:

Android中OAuth 1.0a(单腿)身份验证的示例代码是什么?有图书馆吗?.我用eclipse,我是android的新手.谁能为我澄清道路?

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?

回答我自己的问题:

  1. 下载Scrib.jar库并将其添加到您的lib文件夹(您可以从(
  1. download Scrib.jar library and add it to your lib folder(you can download it from (here)
  2. 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;
}
}


现在您可以进行OAuth身份验证:

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();
            }

  • 请注意,如果您不在AsyncTask中使用上述代码,则将request.send()部分放在线程中(实际上是整个try_catch部分),以避免在主线程异常中运行

  • 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);  
    

    包含以下几行:

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

  • WooCommerce文档站点
    中的更多信息希望对您有所帮助;)
    祝你好运.

    more information in WooCommerce Documentation site
    Hope it help ;)
    good luck..