oAuth 1.0 获取请求改造 android [英] oAuth 1.0 get request retrofit android
本文介绍了oAuth 1.0 获取请求改造 android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 Retrofit 实现 oAuth 1.0 获取请求,但无法获得响应.
I want to implement oAuth 1.0 get request using Retrofit but not able to get response.
邮递员回复:
{
"1": {
"entity_id": "1",
"parent_id": "0",
"position": "0",
"level": "0",
"name": "Root Catalog"
},
"2": {
"entity_id": "2",
"parent_id": "1",
"position": "1",
"level": "1",
"name": "Default Category",
"is_active": "1"
}
}
我的界面是:
@GET("/api/rest/categories/?")
@Headers({ "Content-Type: application/json"})
Call<ResponseBody> getProduct( @Field("oauth_consumer_key") String grantType,
@Field("oauth_token") String username,
@Field("oauth_signature_method") String sig,
@Field("oauth_timestamp") String timS,
@Field("oauth_nonce") String nonc,
@Field("oauth_version") String vers,
@Header("Authorization") String authorization);
方法调用是:
私有 void callService(){
private void callService(){
byte[] credentials = "CONSUMER_SECRETE:TOKEN_SECRETE".getBytes();
String basicAuth = "Basic " + Base64.getEncoder().encodeToString(credentials);
UserClient apiService = ApiClient.getClient().create(UserClient.class);
Call<ResponseBody> call = apiService.getProduct("CONSUMER_KEY","TOKEN_KEY","HMAC-SHA1","1502218838","RuH2FG","1.0",basicAuth);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody>call, Response<ResponseBody> response) {
if (response.isSuccessful()){
if (response.isSuccessful()){
}else{
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
}
}else{
//First Option of error handling..
switch(response.code()){
case 404:
Toast.makeText(getApplicationContext(),R.string.server_error404, Toast.LENGTH_SHORT).show();
break;
case 500:
Toast.makeText(getApplicationContext(),R.string.server_error500, Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getApplicationContext(),R.string.server_errorUnknow, Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<ResponseBody>call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
我还尝试了什么:Android OAuth Retrofit 访问令牌请求
还尝试了其他类型,但都没有奏效.
and also try other types, but none of them have worked.
谁能帮我解决这个问题?我想通过改造来实现这一点.
Can any one help me to fix this issue? i want to implement this with retrofit.
推荐答案
使用这个库 https://github.com/pakerfeldt/okhttp-signpost
public class ApiClient {
public static Retrofit build() {
//untuk oauth ver 1 methode
OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(Constant.CONSUMER_KEY, Constant.CONSUMER_SECRET);
consumer.setTokenWithSecret("", "");
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new SigningInterceptor(consumer))
.build();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL_WEATHER)
.client(client)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
return retrofit;
}
}
这篇关于oAuth 1.0 获取请求改造 android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文