5

我正在开发我的 WooCommerce 商店的 Android 应用程序,我正在使用WooCommerce REST Api的 GET http 动词获取产品、类别、订单、客户等商店数据。它工作正常,我能够为 api V2和 V3 正确生成 oAuth 1.0 签名。现在,我想执行写入操作。我从相同的文档中了解到我需要使用 POST Http 动词。我尝试了同样的方法并卡住了。

当我使用 URL、oAuth 数据和生成的签名对 HttpGet 或 HttpPost 请求执行任何 POST 操作时,我得到:

{"errors":[{"code":"woocommerce_api_authentication_error","message":"Invalid Signature - provided signature does not match"}]}

我正在遵循文档中给出的所有说明以及在 Google 上找到的所有说明,使用“POST”字符串生成 oAuth 签名,尝试使用 HttpGet 和 HttpPost 发送参数但失败了。

谁能给我一些说明或示例,以使用 Android 的 POST Http 动词使用 WooCommerce REST API 执行写入操作。(如创建新订单、创建新类别等)

4

1 回答 1

3

我遇到了同样的错误,我必须做的是创建一个不同的 POST 适配器类。我正在对网络调用进行改造,这是我的代码段:

package me.gilo.a55thavenue.data;

import android.util.Base64;
import android.util.Log;

import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;

/**
 * Created by Aron on 10/31/2015.
 */
public class PostRestAdapter {

    static String oauth_nonce = "";
    static String oauth_timestamp = "";
    static String oauth_signature_method = "HMAC-SHA1";

    static ArrayList<NameValuePair> params;

    public static API createAPI(final String endpoint) {

        setParams(endpoint);

        // Define the interceptor, add authentication headers
        Interceptor interceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {

                HttpUrl.Builder builder = chain.request().httpUrl().newBuilder();
                for (NameValuePair entry : params) {
                    builder.addQueryParameter(entry.getName(), entry.getValue());
                }

                Request newRequest = chain.request()
                        .newBuilder()
                        .url(builder.build())
                        .build();

                return chain.proceed(newRequest);
            }
        };


        // Add the interceptor to OkHttpClient
        OkHttpClient client = new OkHttpClient();
        client.interceptors().add(interceptor);


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API.BASE_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
        return retrofit.create(API.class);
    }

    public static ArrayList<NameValuePair> setParams(String endpoint) {

        final String uri = API.BASE_URL + endpoint;

        oauth_nonce = getOauth_nonce();
        oauth_timestamp = getOauth_timestamp();

        params = new ArrayList<>();
        params.add(new BasicNameValuePair("oauth_consumer_key", API.CONSUMER_KEY));
        params.add(new BasicNameValuePair("oauth_nonce", oauth_nonce));
        params.add(new BasicNameValuePair("oauth_timestamp", oauth_timestamp));
        params.add(new BasicNameValuePair("oauth_signature_method", oauth_signature_method));

        Collections.sort(params, new SortParams());

        String encodedParams = URLEncodedUtils.format(params, "utf-8");
        Log.d("encodedParamString", encodedParams);

        String string_to_sign = "";
        try {
            string_to_sign = (new StringBuilder("POST&")).append(URLEncoder.encode(uri, "utf-8")).append("&").append(URLEncoder.encode(encodedParams, "utf-8")).toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Log.d("string to sign", string_to_sign);

        try {
            Mac mac = Mac.getInstance("HMAC-SHA1");
            String secret = API.CONSUMER_SECRET;
            if (API.WP_API_VERSION.equals("3")) {
                secret = API.CONSUMER_SECRET + "&";
            }
            mac.init(new SecretKeySpec(secret.getBytes("utf-8"), "HMAC-SHA1"));
            String signature = Base64.encodeToString(mac.doFinal(string_to_sign.getBytes("utf-8")), 0).trim();
            Log.d("signature", signature);
            params.add(new BasicNameValuePair("oauth_signature", signature));
        } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return params;
    }

    public static String getOauth_nonce() {
        return (new StringBuilder(String.valueOf(Math.random() * 100000000D))).toString();
    }

    public static String getOauth_timestamp() {
        long stamp = (long) (System.currentTimeMillis() / 1000D);
        Log.d("stamp", stamp + "");
        return (new StringBuilder(String.valueOf(stamp))).toString();
    }

    static class SortParams implements Comparator<NameValuePair> {

        @Override
        public int compare(NameValuePair nameValuePair1, NameValuePair nameValuePair2) {
            return nameValuePair1.getName().compareTo(nameValuePair2.getName());
        }
    }
}

[来源:https://gist.github.com/Aroniez/41dbc5942f70641b397e]

于 2016-03-01T07:30:52.213 回答