Auth 1.0 oauth_signature 创建Android for magento API [英] Auth 1.0 oauth_signature creation Android for magento API
问题描述
我使用以下身份验证作为标头调用 Magento API,
I call the Magento API with the following Autherization as header,
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 时,出现错误 oauth_problem=signature_invalid
.所有其他参数验证成功,但在签名中出现错误,我尝试使用以下代码生成签名,
While I call the API,
Getting error oauth_problem=signature_invalid
.All other parameters validate successfully but got an error in the signature,
I try the following code to generate the signature,
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);
}
我通过 oauth_consumer_secret
和 oauth_token_secret
作为参数来获取签名.但它仍然得到同样的错误.
I pass the oauth_consumer_secret
and oauth_token_secret
as the parameter to get signature . But its still get the same error.
如何在android中生成签名以及我需要传递哪个值才能得到相同的值?
How to generate the signature in android and which value I need to pass to get the same?
推荐答案
我们不需要把所有的属性都作为 auth 传递,retrofit 自己处理这个,我们只需要传递 CONSUMER_KEY、CONSUMER_SECRET、ACCESS_TOKEN 和 TOKEN_SECRET.
We didn't need to pass all the attribute as auth, retrofit itself handle this, we need to pass only the CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN and TOKEN_SECRET.
通过关注this
ApiUtils 类将类似于,
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);
}
}
和 RetrofitClient 类
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
}
}
}
安卓 java
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;
}
}
这篇关于Auth 1.0 oauth_signature 创建Android for magento API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!