Retrofit Bearer token - android

I try to receive token via POST json {"email":"test#example.com","password":"test"}. In postman it works:
Postman request.
I try do the same in Android Studio.
I create class Token:
public class Token {
#SerializedName("token")
#Expose
public String token;
}
And class APIclient
class APIClient {
private static Retrofit retrofit = null;
static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl("http://mybaseurl.com/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
and interface APIInterface:
interface APIInterface {
#POST("/authenticate")
Call<Token> getLoginResponse(#Body AuthenticationRequest request);
}
and class AuthenticationRequest:
public class AuthenticationRequest {
String email;
String password;
}
In onCreate in MainActivity:
apiInterface = APIClient.getClient().create(APIInterface.class);
authenticationRequest.email="test#example.com";
authenticationRequest.password="test";
getTokenResponse();
And here is my getTokenResponse method:
private void getTokenResponse() {
Call<Token> call2 = apiInterface.getLoginResponse(authenticationRequest);
call2.enqueue(new Callback<Token>() {
#Override
public void onResponse(Call<Token> call, Response<Token> response) {
Token token = response.body();
}
#Override
public void onFailure(Call<Token> call, Throwable t) {
call.cancel();
}
});
}
And this is what I see in Logcat:
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: --> POST http://mybaseurl.com/authenticate http/1.1
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: Content-Type: application/json; charset=UTF-8
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: Content-Length: 46
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: {"email":"test#example.com","password":"test"}
03-15 10:53:56.579 20734-20756/com.retrofi2test D/OkHttp: --> END POST (46-byte body)
Could you tell me what I'm doing wrong? I need to give token every time when I'd like to get information from server via GET method.
How can I receive and save token in Android code?

Try this
interface APIInterface {
#POST("/authenticate")
Call<Token> getLoginResponse(#Header("Authorization") String token, #Body AuthenticationRequest request);
}
token is the Bearer token

you should add application/json to header
interface APIInterface {
#Headers({"Content-Type: application/json", "Accept: application/json"})
#POST("/authenticate")
Call<Token> getLoginResponse(#Body AuthenticationRequest request);
}

To get access token from you need to get header from response and read values from headers.
Callback<User> user = new Callback<User>() {
#Override
public void success(User user, Response response) {
List<Header> headerList = response.getHeaders();
//iterating list of header and printing key/value.
for(Header header : headerList) {
Log.d(TAG, header.getName() + " " + header.getValue());
}
}
}
After you get the value you need from header i.e. AccessToken, you can store that value in Storage. e.g. SharedPreference
So next time when you need that value you can directly get that from Storage. e.g. SharedPreference
Now when you pass request for any webservice either that is GET or POST or any other method. You have to pass that into header requests.
#GET("/tasks")
Call<List<Task>> getTasks(#Header("Content-Range") String contentRange);
Or If you need to pass it every time, you can directly pass that from your Retrofit class.
Request request = original.newBuilder()
.header("User-Agent", "Your-App-Name")
.header("Accept", "application/vnd.yourapi.v1.full+json")
.method(original.method(), original.body())
.build();

Related

Retrofit is returning cached response

I am using Retrofit in an Android application. When I hit an API with token to get user information it gives cached(previous) response. Whenever I logged out and log in again API gives previous user detail, I tested API in Postman it works fine there.
I have tried some solutions I searched but nothing is working.
Response header that I am getting is
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Kestrel
Date: Mon, 08 Jan 2018 09:35:26 GMT
Below is ApiClient class
public class ApiClient {
public static final String BASE_URL = "http://XX.XXX.XXX.XX/api/";
private static Retrofit authRetrofit = null;
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
return retrofit;
}
public static Retrofit getAuthorizeClient(final String token) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.addHeader("Authorization", "Bearer " + token)
.addHeader("Cache-control", "no-cache")
.method(original.method(), original.body())
//.cacheControl(CacheControl.FORCE_NETWORK)
.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.cache(null).build();
if (authRetrofit == null) {
authRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.client(client).build();
}
return authRetrofit;
}
}
In your httpClient interceptor, try adding cache control header:
.addHeader("Cache-control", "no-cache");
EDIT
Just noticed you are on Retrofit2
original.newBuilder().header("Cache-control", "no-cache");
Alternatively try adding it as an annotation to your API method:
#Headers("Cache-control: no-cache")
Response callApi();
According to Docs.
EDIT 2
Okay I suspect it's because of your if condition, your authRetrofit wouldn't be updated if condition failed. Try removing it
if (authRetrofit == null)
Help this helps.
Use Post request instead, I've faced the same problem, rectified by changing my GET method to post and send one random string as a Field.

Retrofit gets 400 Bad request but works with postman

My api base url is:
https://locodealapi.herokuapp.com/api/deals
In postman pass following in header and it works fine.
x-access-token:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX25hbWUiOiJkZWFsQWRtaW4iLCJ1c2VyX3Bhc3N3b3JkIjoiY2U5MDZkNDkzMWY3NmFhZWFmZTViNjM0YTg3Y2RkYzQ4YjMzNmUzYmVlZDU1N2ZhZTI0YzMxZTY3YmQyN2NhZDg4NDJmNzJlNTJlNGUyODc5ZWVkODY1MDljYzY5ODJlNDYwMjU0Mzk4ODQzMTljMTQxMGMwOTcxNGU0Y2MxMDNiMDQwYjkwOTQ1OTMyMjYwMGI1MjgwMmRkOTc2YTE4OGE5MTZiMWU4ZmU1YmY1ZWZiNTlkYjc2NDhmMjQxOTg0ODdiMWE5OGVhMjg2ZWZjZDI1M2Y5ZWUxNTQ0OTI3ODZiYjY3NzNmZTZjNGY3ODhmNzJjMzQ0NTEwMzg2NjkyNzIzMjNiMjZmYjFhMmUzYjE0ZTQ0Nzc0NzljMTExMzNkZjIwMmE3ODY2OGIxZjdiY2NlZjQ5NTM0YTJhYzk5N2Y0MTc0NzU2OTkwOTg5NjQ1YzljNWQxYTZkYmUyZTI1ZjFlNDE3YjdhYWI1N2QyZGJhNmVmOWEwNzk5N2Q5ZDcyNTgxMzUyYzc4Y2IxY2RiMDRmNDFkZjMwNzdjYzAxMGM2YmYzYWMzOTQ5OTJjN2Y3ZGVmYmE3YzllZWMxMzI5ODhlODBiM2RmYWE5NTVhMzE3NTgxNThiZjYxYjYyN2U4Y2UzOTg2NmRkODk2YzBlZDkzNDJlMzhiMzhkM2Q0ZTY0YWIzZWIwNGU4YmU5MGQwNmM2NTg2OWYyNzZkMzgyOTU3MTFkODgyNjI4NGIxMjkzYzQ0ZGNjMWM3ZjU2YmE4OGM5MTkyNDMzZWY0YTcwZGE2NTkzMzAwZmUzMGMxYjkwOWY5YmJjMDhmY2M5Zjk3NjUwYmJkOTVhNzExNWVlOGY3MjI5N2Q4ZGY4MjZiMTk0MTBlZjQ0OWZkMTQ1NTg3Nzc2NjUxZDI5OGMyYzMwMTZiMDMxMGEwZjRlMWQ4NzgwZTExYmVkOTZhYzg3MDU2YzIwYWU4ODZiYjI3NGZkNGVhZGMzMjQ0ZjlmMmZkMGIyMGMzZTAyZjI1NGQ3OWYzYzhiM2FmZGJiM2UzYTM1YTA2MjNjMTQwY2JmNzMxZWMxYjE2Y2VmNmIwY2RhMjQ1YjkzZmY4NzQ1MGRjNWFhZDE2MzdkNTQ5ZjM5ZDkwZjk5MWFiNWMxNTcwNWExYWJjYWQxYzQyYjNmMTViN2Y5N2I0YTc1ZDA2YTgwYjkwOWI0ZDJlZjRiNWE0NzdhMTMzYWI0ODkwMmIyNWFiM2VmYTMzNDdjMDcxODQ4NmMwYmU2NGQxYzUxMjA1ZTMwYjU5NjY3Yjk5ZTVlYTdkZWNiMWE4MWRkNjcyYzEwMzU5YmM2NGNlZWI3ZTEyOWJlYjZjYWUzYTM0ZjM0M2NiYjk4NyIsImNyZWF0ZXRpbWUiOm51bGwsImZpcnN0X25hbWUiOm51bGwsImxhc3RfbmFtZSI6bnVsbCwiYWdlIjpudWxsLCJzZXgiOm51bGwsImRvYiI6bnVsbCwic3RhdHVzIjpudWxsLCJpdGVyYXRpb24iOjEwMDAwLCJ1c2VyX3R5cGUiOiIwIiwidXNlcl9pbWFnZSI6bnVsbCwic2FsdCI6Ikh1dm9VQ3FRak81Mi9BbkJIdmp2MldEMERod2JmM0R4ZXV6ZWJMSlFYK3hZTUptMmRJSUw3VFYvcnFVVXFVNUpmRlY0dHhkMHNCcXhLbnVNakU2YlNzVnpaQ0dWVXg3YmY4MGNubTR5WnB5OElEUWowY1crTXhCUXdvMFNja2UzT0FnVk4zd2pnNmlxSVk3VnJwRmw1WTNLRzFwY1NMelZjREJiME9XdXdjcz0iLCJpYXQiOjE1MTAxMjY1OTUsImV4cCI6MTUxODc2NjU5NX0.5XFJnJqTsfID9uqOwkNf46oraj9jDxic7qNSqBdunD0
In retrofit Interface I have following but get 400 Bad request
#POST("api/deals")
Call<ResponseBody> deals(#Header("x-access-token") String x_access_token)
Calling code:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://locodealapi.herokuapp.com")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
AppRestAPI client = retrofit.create(AppRestAPI.class);
Call<ResponseBody> call1 = client.deals(
token
);
call1.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.i(TAG, "The response is " + response.message());
Log.i(TAG, "The response is " + response.body());
try {
Log.i(TAG, "The response is " + response.errorBody().string());
if (response.code() == 400) {
Log.v("Error code 400",response.errorBody().string());
}
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e(TAG, "onFailure: Failed", t);
}
});
OKHTTP Log interceptor
11-21 21:14:30.939 3253-3342/? D/OkHttp: --> POST https://locodealapi.herokuapp.com/api/deals
11-21 21:14:30.939 3253-3342/? D/OkHttp: Content-Length: 0
11-21 21:14:30.940 3253-3342/? D/OkHttp: x-access-token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoyNSwidXNlcl9uYW1lIj....
11-21 21:14:30.942 3253-3342/? D/OkHttp: --> END POST (0-byte body)
11-21 21:14:34.188 3253-3342/? D/OkHttp: <-- 400 Bad Request https://locodealapi.herokuapp.com/api/deals (3246ms)
11-21 21:14:34.189 3253-3342/? D/OkHttp: Connection: close
11-21 21:14:34.189 3253-3342/? D/OkHttp: Server: Cowboy
11-21 21:14:34.189 3253-3342/? D/OkHttp: Date: Tue, 21 Nov 2017 15:29:33 GMT
11-21 21:14:34.190 3253-3342/? D/OkHttp: Content-Length: 0
11-21 21:14:34.191 3253-3342/? D/OkHttp: <-- END HTTP (0-byte body)
11-21 21:14:34.196 3253-3253/? I/BaseDrawerActivity: The response is Bad Request
11-21 21:14:34.196 3253-3253/? I/BaseDrawerActivity: The response is null
11-21 21:14:34.196 3253-3253/? I/BaseDrawerActivity: The response is
Note:
The same code works fine in local nodejs server (http only)
1.Frist thing your api is a GET Method so use #GET instead #POST
Second try to change url base url in retrofit
.baseUrl("https://locodealapi.herokuapp.com")
to .baseUrl("https://locodealapi.herokuapp.com/")
this will work. or leave your problem in comment
2.this is sample code
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(
new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException{
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder =original.newBuilder().
method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
})
.addInterceptor(interceptor).connectTimeout(60,TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://locodealapi.herokuapp.com/")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient).build();
UserApi userApi = retrofit.create(UserApi.class);
Call<ResponseBody> call = userApi.deals("your token");
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call,retrofit2.Response<ResponseBody> response) {
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {}
});
}
#GET("api/deals")
Call deals(#Header("x-access-token") String x_access_token);
Greeting of the day!
Try this:
#FormUrlEncoded
#POST("/api/deals")
Call<ResponseBody> deals(#Header("x-access-token") String x_access_token, #Field("<parameter_name>") String parameter);
Call api as below:
Call<ResponseBody> call = client.deals(token,"<parameter>");
Here, i am assuming that the API has a parameter which can be passed as 2nd argument in the method deals(). You can pass multiple parameters as the arguments of the method.
Refer the following link for more details:
https://futurestud.io/tutorials/retrofit-send-data-form-urlencoded
I hope, this solves your problem. If not, please provide complete details of the API that you want to call.
A 400 response means that it's a disagreement between your request and the server, but that the request was created, sent, and a response was parsed. Which means Retrofit is working fine. You can use OkHttp's logging interceptor to log the raw request and compare it with what the server expects.
Also your Response size is around 5.96 MB. This is too big response. Instead of receiving this much of data in single response you could implement pagination or something similar to break down data. This could be one of the reason.
//Your Main connection Class
public class ApiClient {
public static final int DEFAULT_TIMEOUT_SEC = 90;
public static OkHttpClient client;
private static Retrofit retrofit = null;
public static Retrofit getClient() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.connectTimeout(DEFAULT_TIMEOUT_SEC, TimeUnit.SECONDS)
.readTimeout(DEFAULT_TIMEOUT_SEC, TimeUnit.SECONDS)
.writeTimeout(DEFAULT_TIMEOUT_SEC, TimeUnit.SECONDS);
httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
System.out.println("API_KEY"+PrefManager.getActiveInstance(RecrouteCandidateApplication.getsCurrentContext()).getDeviceToken());
Request request = original.newBuilder()
.header("x-access-token",YOUR_ACCESS_TOKEN"")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
//For logging the call on Logcat
HttpLoggingInterceptor interceptor1 = new HttpLoggingInterceptor();
interceptor1.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(interceptor1);
client = httpClient.build();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(AppConstants.BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
Use in Your Class which will initiate the connection like this
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
And This will be your ApiInterface
public interface ApiInterface {
#POST("Your url here")
Call<JsonObject> sampleMethod();
/*For non-empty body use this*/
//Call<JsonObject> getSignUpResponse(#Body JsonObject register);
//For form data use this
#FormUrlEncoded
#POST("Your url here")
Call<String> sampleMethod1(#Field(value= "param1_key", encoded = true) String param1_value);
}
Make a call like this for json
JsonObject obj= new JsonObject();
obj.addProperty("key1","keyvalue");
Call<JsonObject> call = apiService.sample1(obj);
call.enqueue(new Callback<JsonObject>() {
#Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.body().get("status").getAsString().equals("1")) {
Toast.makeText(context, response.body().get("msg").getAsString(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, response.body().get("msg").getAsString(), Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Toast.makeText(context, AppConstants.NO_DATA_AVAILABLE, Toast.LENGTH_LONG).show();
}
});
for me the problem was because I had this line in the Gson builder :
.excludeFieldsWithoutExposeAnnotation()
with the above line, all the fields in your model classes that you send as body in retrofit will be ignored if they are not annotated with the #Exposed annotation.
removing .excludeFieldsWithoutExposeAnnotation() solved the problem for me.

Retrofit 2 prints empty response body in log with HttpLoggingInterceptor.Level.BODY

I use Retrofit 2.0.0. This is the code that builds my HTTP client
protected OkHttpClient getHttpClient(){
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(
chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder();
requestBuilder.header("Accept-Language", App.getInstance().getPrefs().getSelectedLanguage().toLowerCase());
requestBuilder.header("Accept-Encoding", "gzip");
if(API.this instanceof PrivateAPI){
LoginResponse loginResponse = AuthManager.getInstanse().getLoginResponse();
requestBuilder.header("Authorization", String.format("%s %s",
loginResponse.getTokenType(),
loginResponse.getAccessToken()));
}
requestBuilder.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
});
if(BuildConfig.DEBUG){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(loggingInterceptor);
}
return builder.build();
}
And here is how I use it to init my Retrofit API interface:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(getGson()))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(getHttpClient())
.build();
apiService = retrofit.create(PrivateAPIInterface.class);
Until yesterday I was getting full log in logcat as it supposed to be. But suddenly I begin to get empty response bodies in my log. So here is an example of log I get now:
D/OkHttp: --> GET http://MY_DOMAIN/utility/gaz?branchid=&phone=21919&customerid= HTTP/1.1
D/OkHttp: Accept-Language: hy
D/OkHttp: Accept-Encoding: gzip
D/OkHttp: Authorization: 0yXK65Go_hRPT32WIP4DAKjmzIaM0riSLlybHhusvwmYJSxTdTVBrkL5CaqopXgrOuNFL5xvR5uVwCbprWrxvfYTMJ1I_AUmd5TKdNL4ptkUrJuOy1kGiule_yveCFv0GbTiAXhYjdfynJICJEVWc54ibnIv_Q14dLpPuVle5IaRuHmza2Pavkrhzi42sfMsK0Qpxaueu8eRPysk6MP9WkTnSKYIAThuq3sHq8RGAnkaLBx
D/OkHttp: --> END GET
D/OkHttp: <-- 200 OK http://MY_DOMAIN/utility/gaz?branchid=&phone=21919&customerid= (394ms)
D/OkHttp: Content-Length: 8527
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: Server: Microsoft-IIS/7.5
D/OkHttp: X-Powered-By: ASP.NET
D/OkHttp: Date: Wed, 16 Mar 2016 12:30:37 GMT
D/OkHttp: OkHttp-Sent-Millis: 1458131438086
D/OkHttp: OkHttp-Received-Millis: 1458131438438
D/OkHttp: }
D/OkHttp: <-- END HTTP (8527-byte body)
As you can see everything I get instead of response body is D/OkHttp: }.
But in Postman I see a normal response body and in my app everything works fine so the problem is only with logging.
Can anybody help me to understand why this can start happening?
P.S. In case of requests that has body I see the request body the problem is only with response bodys.
With retrofit-2 you are supposed to use OkHttp3 for getting full logs. Add this to your dependecies: compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://example.com")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(ApiClient.class);
ResClient.class
public class RestClient {
private static ApiInterface apiInterface;
static {
setupRestClient();
}
private static void setupRestClient() {
OkHttpClient httpClient = new OkHttpClient();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(NetworkConstants.URL_BASE_LIVE)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setLog(new AndroidLog("Retrofit"))
.setClient(new OkClient(httpClient))
.build();
apiInterface = restAdapter.create(ApiInterface.class);
}
public static ApiInterface getAdapter(){
return apiInterface;
}
}
ApiInterface.class
public interface ApiInterface {
// result api
//All result api async
#Headers({"Authorization: ######################"}) //for the header authorization if needed.
#GET(NetworkConstants.URL_GET_RESULTS)
void getResult(#QueryMap Map <String, String> params, Callback<ArrayList<Results>> callback);
}
Now call it like
// Fetching result through Api Hit
RestClient.getAdapter().getResult(getJobsParams(), new Callback<ArrayList<Result>>() {
#Override
public void success(ArrayList<Result> result, retrofit.client.Response response) {
// Do your Work
}
#Override
public void failure(RetrofitError error) {
// show server or other error to user
}
});

Convert cURL to Retrofit

I am trying to implement OAuth2. I need to use it for my school project - the school provide some documentation and it say that I need to call this
POST /oauth/token HTTP/1.1
Host: oaas.example.org
Authorization: Basic ZHVtbXktY2xpZW50OnRvcC1zZWNyZXQ=
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=l1kSf2&redirect_uri=https://client.example.org/auth
or there is sample with curl
curl --data "grant_type=authorization_code&code=l1kSf2&redirect_uri=https://client.example.org/auth" \
--user dummy-client:top-secret https://oaas.example.org/oauth/token
But I dont know how to transfer it to Android calling. I am using Retrofit currently and reguest is like JSON :
POST /oauth/oauth/token/DeviceAndroid HTTP/1.1
Authorization: Basic OGRkOGJiZGMtOGI2NC00NTdlLTgwYWMtZmE5NDZjNzY4Njgzc0owY3RKV2VWNHFLY0dwWHNIOGEzcDVYYUl0NDRGN2o=
Content-Type: application/x-www-form-urlencoded
Content-Length: 85
Host: auth.fit.cvut.cz
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/2.5.0
{"code":"p1P3os","grant_type":"authorization_code","redirect_uri":"http://localhost"}
My network interface
public interface NetworkInterface {
String TAG = NetworkInterface.class.getName();
String URL = "https://auth.fit.cvut.cz";
#Headers("Content-Type: application/x-www-form-urlencoded")
#POST("/oauth/oauth/token/DeviceAndroid")
Call<Object> sendInformationToServer(#Body RequestToken requestToken);
}
RequestToken
public class RequestToken {
public static final String TAG = RequestToken.class.getName();
public String grant_type;
public String code;
public String redirect_uri;
...
Service generator
public class ServiceGenerator {
private static OkHttpClient httpClient = new OkHttpClient();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(NetworkInterface.URL)
.addConverterFactory(GsonConverterFactory.create());
public static <T> T createService(Class<T> serviceClass , final String autorizationString) {
httpClient = new OkHttpClient();
if (autorizationString != null) {
httpClient.interceptors().clear();
httpClient.interceptors().add(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Authorization", "Basic " + RequestToken.returnBase64(autorizationString))
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
}
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
}
}
Is there a way to call it right with Retrofit (or with different approach), Thanks in advance
It seems like your call should look like the following
public interface NetworkInterface {
private String TAG = NetworkInterface.class.getName();
public statsic final String ROOT_URL = "https://auth.fit.cvut.cz";
#FormUrlEncoded
#POST("/oauth/token")
Call<Object> sendInformationToServer(
#Field("code") String code,
#Field("grant_type") String grant,
#Field("redirect_uri") String redirect,
);
}
Used like this:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(NetworkInterface.ROOT_URL)
...
.build();
NetworkInterface service = retrofit.create(NetworkInterface .class);
service.sendInformationToServer("p1P3os", "authorization_code", "http://localhost"})

Android Retrofit 2.0.0-beta2 Post string body dynamic headers

Retrofit 2.0.0-beta2
#Headers({
"Authorization: {authorization}",
"Content-Type: application/json"
})
#POST("/api/{id}/action/")
Call<String> postfn(#Header("Authorization") String authorization, #Path("id") String id, #Body String body);
i am using Gson converter
.addConverterFactory(GsonConverterFactory.create(gson))
i am getting error code=400, message=Bad Request Should i use a custom
converter?
Please help
You can't put this two things together.
There are two ways to put dynamic headers on requests with retrofit 2.0
1: put it only in method signature
#Headers({
"Content-Type: application/json"
})
#POST("/api/{id}/action/")
Call<String> postfn(#Header("Authorization") String authorization, #Path("id") String id, #Body String body);
2: using request interceptor to add fixed dynamic headers
public class TraktvApiProvider implements Provider<TraktvApi> {
public static final String BASE_URL = "https://api-v2launch.trakt.tv/";
#Override
public TraktvApi get() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(JacksonConverterFactory.create())
.build();
retrofit.client().interceptors().add(new LoggingInterceptor());
return retrofit.create(TraktvApi.class);
}
private class LoggingInterceptor implements Interceptor {
#Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
request = request.newBuilder()
.addHeader("trakt-api-version", "2")
.addHeader("trakt-api-key", "[YOUR-API-KEY]")
.build();
Response response = chain.proceed(request);
String bodyString = response.body().string();
Log.d("Retrofit", "---------------------------------- REQUEST ----------------------------------");
Log.d("Retrofit", String.format("%s - %s", request.method(), request.url()));
Log.d("Retrofit", request.headers().toString());
Log.d("Retrofit", "---------------------------------- REQUEST ----------------------------------");
Log.d("Retrofit", "---------------------------------- RESPONSE ----------------------------------");
Log.d("Retrofit", response.headers().toString());
Log.d("Retrofit", "Body: " + bodyString);
Log.d("Retrofit", "---------------------------------- RESPONSE ----------------------------------");
return response.newBuilder()
.body(ResponseBody.create(response.body().contentType(), bodyString))
.build();
}
}
}
I moved back to the stable version 1.9 and intercepting worked fine.

Categories

Resources