使用okhttp-signpost进行OAuth签名修改可获得特殊字符的OAuthMessageSignerException

时间:2018-03-15 09:31:50

标签: android oauth retrofit2 okhttp3 signpost

我正在使用Retrofit 2在Android应用中发出http请求。我与之交谈的服务器需要OAuth 1.0授权。我从这里使用okhttp-signpost来处理OAuth签名。

这是我的build.gradle库包括:

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'se.akerfeldt:okhttp-signpost:1.1.0'
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
compile 'oauth.signpost:signpost-core:1.2.1.2'

MyApi课程中,我将dailyChart定义为Retrofit GET request

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

@GET("chart")
Call<ChartResponse> dailyChart(@Query("symbol") String symbol);

以下是我如何制作dailyChart() GET request

import se.akerfeldt.okhttp.signpost.OkHttpOAuthConsumer;
import se.akerfeldt.okhttp.signpost.SigningInterceptor;

// for OAuth signing
OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);

OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(new SigningInterceptor(consumer))
        .build();

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(.....)
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build();

MyApi myApi = retrofit.create(MyApi.class);

String symbol = '^KKKL';
Call<ChartResponse> call = myApi.dailyChart(symbol);

但由于我^中的^KKKL字符我改为@Query参数,因此我收到以下错误:

D/OkHttp: --> GET https://......chart?symbol=^KKKL http/1.1
D/OkHttp: --> END GET
D/OkHttp: <-- HTTP FAILED: java.io.IOException: Could not sign request
E/.......: java.io.IOException: Could not sign request
at se.akerfeldt.okhttp.signpost.SigningInterceptor.intercept(SigningInterceptor.java:48)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:211)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
at okhttp3.RealCall.execute(RealCall.java:69)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:91)
.......
Caused by: oauth.signpost.exception.OAuthMessageSignerException: java.net.URISyntaxException: Illegal character in query at index ...: https://.....chart?symbol=^KKKL
at oauth.signpost.signature.SignatureBaseString.generate(SignatureBaseString.java:60)
at oauth.signpost.signature.HmacSha1MessageSigner.sign(HmacSha1MessageSigner.java:51)
at oauth.signpost.AbstractOAuthConsumer.sign(AbstractOAuthConsumer.java:109)
at oauth.signpost.AbstractOAuthConsumer.sign(AbstractOAuthConsumer.java:120)
at se.akerfeldt.okhttp.signpost.SigningInterceptor.intercept(SigningInterceptor.java:46)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:211) 
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185) 
at okhttp3.RealCall.execute(RealCall.java:69) 
at retrofit2.OkHttpCall.execute(OkHttpCall.java:180) 
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:91) 
.......
at oauth.signpost.signature.SignatureBaseString.normalizeRequestUrl(SignatureBaseString.java:65)
at oauth.signpost.signature.SignatureBaseString.generate(SignatureBaseString.java:54)
at oauth.signpost.signature.HmacSha1MessageSigner.sign(HmacSha1MessageSigner.java:51) 
at oauth.signpost.AbstractOAuthConsumer.sign(AbstractOAuthConsumer.java:109) 
at oauth.signpost.AbstractOAuthConsumer.sign(AbstractOAuthConsumer.java:120) 
at se.akerfeldt.okhttp.signpost.SigningInterceptor.intercept(SigningInterceptor.java:46) 
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:211) 
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185) 
at okhttp3.RealCall.execute(RealCall.java:69) 
at retrofit2.OkHttpCall.execute(OkHttpCall.java:180) 
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:91) 

所以我尝试url-encode symbol,然后传入Retrofit:

String symbol = '^KKKL';
try {
    query = java.net.URLEncoder.encode(symbol, "UTF-8");
} catch (UnsupportedEncodingException ex) {
    throw new StockHistoryNotFoundException(null, ex);
}

Call<ChartResponse> call = myApi.dailyChart(symbol);

然后我又得到了另一个错误,如下所示。我认为Retrofit再次编码我传入的已编码@Query参数。

D/OkHttp: <-- 404 Not Found https:.....chart?symbol=%255EKKKL

任何人都知道对此有任何解决方法吗?

1 个答案:

答案 0 :(得分:0)

我最终解决了这个问题。在Retrofit 2 documentation中,有一个名为encoded的可选元素。它的作用是:

  

<强>编码

     

指定参数名称和值是否已为URL   编码。

MyApi课程中,我将@Query symbol更改为使用encoded=true

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

@GET("chart")
Call<ChartResponse> dailyChart(
    @Query(value="symbol", encoded=true) String symbol
);

在来电代码中,在传入symbol之前编码dailyChart

String symbol = '^KKKL';
try {
    query = java.net.URLEncoder.encode(symbol, "UTF-8");
} catch (UnsupportedEncodingException ex) {
    throw new StockHistoryNotFoundException(null, ex);
}

Call<ChartResponse> call = myApi.dailyChart(symbol);

然后okhttp-signpost不再抱怨特殊字符问题,&amp; Retrofit不会对参数进行双重编码。