gpt4 book ai didi

android - 使用 okhttp-signpost 改造 OAuth 签名获取特殊字符的 OAuthMessageSignerException

转载 作者:行者123 更新时间:2023-11-29 02:31:49 25 4
gpt4 key购买 nike

我正在使用 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 请求:

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

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

下面是我如何发出 dailyChart() GET 请求:

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) 

所以我尝试对 symbol 进行url-encode,然后再传递给 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

有人知道解决这个问题的方法吗?

最佳答案

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

encoded

Specifies whether the parameter name and value are already URL encoded.

MyApi 类中,我将 @Query 符号 更改为使用 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
);

在调用者代码中,在传递给 dailyChart 之前对 symbol 进行编码:

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 不再提示特殊字符问题,并且 Retrofit 不会对参数进行双重编码。

关于android - 使用 okhttp-signpost 改造 OAuth 签名获取特殊字符的 OAuthMessageSignerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49295735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com