Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Auth 1.0 oauth_签名创建Android for magento API_Android_Rest_Authorization_Retrofit_Magento 1.9 - Fatal编程技术网

Auth 1.0 oauth_签名创建Android for magento API

Auth 1.0 oauth_签名创建Android for magento API,android,rest,authorization,retrofit,magento-1.9,Android,Rest,Authorization,Retrofit,Magento 1.9,我使用以下身份验证作为头调用Magento API auth = "OAuth oauth_consumer_key=**********************,oauth_consumer_secret=****************,oauth_token=************,oauth_token_secret=**************,oauth_signature_method=HMAC-SHA1,oauth_timestamp=" + ConstantFunctions

我使用以下身份验证作为头调用Magento API

auth = "OAuth oauth_consumer_key=**********************,oauth_consumer_secret=****************,oauth_token=************,oauth_token_secret=**************,oauth_signature_method=HMAC-SHA1,oauth_timestamp=" + ConstantFunctions.GetTimeStamp() + ",oauth_nonce=" + ConstantFunctions.GetNonce() + ",oauth_signature=*******************) ;
当我调用API时, Getting error
oauth\u problem=signature\u invalid
。所有其他参数都已成功验证,但签名中出现错误, 我尝试使用以下代码生成签名

     public static String GETHMACSHA1(String value, String key)
            throws UnsupportedEncodingException, NoSuchAlgorithmException,
            InvalidKeyException {
        String type = "HmacSHA1";
        SecretKeySpec secret = new SecretKeySpec(key.getBytes(), type);
        Mac mac = Mac.getInstance(type);
        mac.init(secret);
        byte[] bytes = mac.doFinal(value.getBytes());
        return bytesToHex(bytes);
    }

    private final static char[] hexArray = "0123456789abcdef".toCharArray();

    private static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        int v;
        for (int j = 0; j < bytes.length; j++) {
            v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
public静态字符串GETHMACSHA1(字符串值,字符串键)
抛出不支持的CodingException、NoSuchAlgorithmException、,
InvalidKeyException{
字符串类型=“HmacSHA1”;
SecretKeySpec secret=新的SecretKeySpec(key.getBytes(),type);
Mac Mac=Mac.getInstance(类型);
mac.init(秘密);
byte[]bytes=mac.doFinal(value.getBytes());
返回bytesToHex(字节);
}
私有最终静态字符[]hexArray=“0123456789abcdef”.toCharArray();
私有静态字符串bytesToHex(字节[]字节){
char[]hexChars=新字符[bytes.length*2];
INTV;
对于(int j=0;j>>4];
hexChars[j*2+1]=hexArray[v&0x0F];
}
返回新字符串(hexChars);
}
我通过
oauth\u consumer\u secret
oauth\u token\u secret
作为参数来获取签名。但它仍然会得到相同的错误


如何在android中生成签名,以及需要传递哪个值才能获得相同的签名?

对于Oauth,我认为您不应该传递CS和TS。您需要连接一组URL编码的属性和参数来构造签名基字符串。请参阅- devdocs.magento.com/guides/v2.0/get-start/authentication/

换句话说,SHA1中的一个参数将是一个编码的url 它应该是以HTTP方法开始的特定格式

编码前,url应包含上述参数

我在WooCommerceAPI中为android做了一个类似的Oauth身份验证。更多信息,请参考此要点url


在此检查中,方法名为
getLoginUrl()
。在其中我连接了url

我们不需要以auth的形式传递所有属性,改型本身可以处理这个问题,我们只需要传递CONSUMER\u密钥、CONSUMER\u SECRET、ACCESS\u令牌和TOKEN\u SECRET

跟随

ApiUtils类将类似于,

科特林 }

就业喜报 对于客户类

科特林 安卓java
我只有消费者密钥和消费者机密。我使用了上面的格式。仍然有401个错误。你使用了Magento 1.9吗@LavanyaVelusamyNo。我没有使用它。你能帮我一下吗?告诉我详细信息,比如你的后端是什么,你尝试了什么,以及你在哪里得到错误@lavanyavelusamybend是.net。他们已经给了消费者密钥,消费者机密,并且必须在请求Url中添加授权数据。我使用了上面的方法,我得到了401个未经授权的访问@Subin Babu
class ApiUtils {
companion object {
    fun getAPIService(): APIService? {
        val consumer = OkHttpOAuthConsumer(BuildConfig.CONSUMER_KEY, BuildConfig.CONSUMER_SECRET)
        consumer.setTokenWithSecret(BuildConfig.ACCESS_TOKEN, BuildConfig.TOKEN_SECRET)
        return RetrofitClient.getClient(BuildConfig.BASE_URL, consumer)?.create(APIService::class.java)
    }
}
public class ApiUtils {

    private ApiUtils() {
    }

    private static final String BASE_URL = BuildConfig.BASE_URL;

    public static APIService getAPIService() {

        OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(BuildConfig.CONSUMER_KEY, BuildConfig.CONSUMER_SECRET);
        consumer.setTokenWithSecret(BuildConfig.ACCESS_TOKEN, BuildConfig.TOKEN_SECRET);

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new SigningInterceptor(consumer))
                .build();
        return RetrofitClient.getClient(BASE_URL, client).create(APIService.class);
    }
}
    class RetrofitClient {
    companion object {
        private var retrofit: Retrofit? = null
        private val gSON = GsonBuilder()
            .setLenient()
            .create()

        fun getClient(baseUrl: String, consumer: OkHttpOAuthConsumer): Retrofit? {
            val logging = HttpLoggingInterceptor()
            if (BuildConfig.DEBUG) {
                logging.level = HttpLoggingInterceptor.Level.BODY
            } else {
                logging.level = HttpLoggingInterceptor.Level.NONE
            }

            val httpClient = OkHttpClient.Builder()
            httpClient.connectTimeout(60000, TimeUnit.SECONDS)
            httpClient.writeTimeout(120000, TimeUnit.SECONDS)
            httpClient.readTimeout(120000, TimeUnit.SECONDS)
            httpClient.retryOnConnectionFailure(true)
            httpClient.addInterceptor(SigningInterceptor(consumer))
            httpClient.addInterceptor { chain ->
                val request = chain.request()
                val requestBuilder = request.newBuilder()
                    .header(HEADER_CONTENT_TYPE_KEY, PreferenceHandler.getContentType())
                    .header(HEADER_ACCEPT_KEY, PreferenceHandler.getAcceptType())
                    .header(HEADER_CACHE_CONTROL_KEY, PreferenceHandler.getCacheControl())
                val modifiedRequest = requestBuilder.build()
                chain.proceed(modifiedRequest)
            }

            httpClient.addNetworkInterceptor(logging)

            if (retrofit == null) {
                retrofit = Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create(gSON))
                    .client(httpClient.build())
                    .build()
            }
            return retrofit
        }

    }
}
public class RetrofitClient {

    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl,OkHttpClient client) {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}