Call Twitter Endpoint API to follow another user Android - android

I'm using Twitter end point API to follow another user. The API is:
https://api.twitter.com/1.1/friendships/create.json?follow=&screen_name=&user_id=
with Authorization header passed as:
#Headers("Authorization: OAuth oauth_consumer_key=DC0sePOBbQ8bYdC8Smg,oauth_signature_method=HMAC-SHA1,oauth_timestamp=1502774524,oauth_nonce=175308858,oauth_version=1.0,oauth_token=712057165-iQB4b4Q0hsNmHsAxiW4X5UF5xVB6JmKOPhxnW,oauth_signature=X0GExH5DBVgVv49jkO3LwfX8%3D")
#POST()
#FormUrlEncoded
Call<ResponseBody> followUser(#Url String url, #Field("follow") boolean follow, #Field("screen_name") String screenName, #Field("user_id") String userId);
in Retrofit API call from Android. This works fine. But the Auth header has to be generated dynamically for every logged in user. How to achieve that?

You can use #Header annotation in parameter list. Please check the official documentation.
Replaces the header with the value of its target.
#GET("/")
Call<ResponseBody> foo(#Header("Accept-Language") String lang);
Header parameters may be null which will omit them from the request.
Passing a List or array will result in a header for each non-null
item.
Note: Headers do not overwrite each other. All headers with the same
name will be included in the request.

Related

Can API keys be added to the header using Retrofit2 android

Cannot get the API request to work with the API key. Have already tested it with a different API that doesn't use an API Key, which has worked. Makes me think that I'm not adding the API Key properly.
Tested it on postman using the authentication tab which works well.
How can I send the key Access-Key and value 9xxxxxxxxxxxxx3 using retrofit2?
code
When you call R.string.api_key, you don't get the String value, you only get its id, which is represented as a number. To get the value you need to have context and call context.getString(R.string.api_key). In our case, it is better to take it out from string.xml and place it in some class.
For example
object Constants {
const val BASE_URL = "http://test.com/"
}
and then inside the retrofit
return Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
but if you want to get the value from string.xml you need to change getInstance() method from example add Context.
You can send Headers like as:
public interface APIService {
//Login
#FormUrlEncoded
#POST("loginAction")
Call<LoginModel> loginMI(
#Field("username") String username,
#Field("pwd") String pwd,
#Header("api_key") String api_key,
#Header("secret_key") String secret_key
);
}
call the above API and pass the required Field and Headers.

What is the difference between #Field and #Query in retrofit

In some POST requests, I don't know when to use #Field with FormUrlEncoded and when to use #Query
For Example:
#POST("list-products-for-sale")
Call<ListAllProductsResponse> getNewProducts(#HeaderMap Map<String,
String> headers,#Query("lastProductId") String lastProductId);
When I tried to use #Field here it was not responding properly and when I switched it to #Query it's working great.
I want to know why #Field isn't working while Query can work perfectly and I did tested in POSTMAN where I sent the data as a formurlencoded and it's giving me the results fin.
EDIT
BTW I'm passing Content-Type:application/json, Accept: application/json with an Authorization key
#Field is used to send #FormUrlEncoded request in Retrofit which hides your parameter and not attach with url to provide security.Used for POST request.
#Query parameter appended to the URL.
If you are using #Field request than it will hides your parameter and not append with the url.
If you are using #Query request than all your parameter is append to your request and visible to users.
Depend on your api request you have to use one of above annotation. If the api request accept the #FormUrlEncoded data than use #Field or if they want to attached it with url than use #Query.
You can get more information from below link :
1) https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Query.html
2) https://square.github.io/retrofit/2.x/retrofit/retrofit2/http/Field.html

Adding dynamic header to POST request with body (Retrofit v2.4.0)

I am using Retrofit v2.4.0 in my project. I need to get token using one API call and using this token in header of the POST request I should use another API call where I should add one header and two parameter to the body of the request. The following code is of the second API call (JavaRx is used):
#FormUrlEncoded
#POST("auth/sendCode")
#Headers("Content-Type: application/json")
Single<SendCodeResponse> sendCode(#Header("token") String token,
#Field("phoneNumber") String phoneNumber,
#Field("langCode") String langCode);
However, it is not working. When I log my request, it is showing that the request body is not correct.
What is wrong with my code above?

How to read custom header response (json object) in retrofit

I have an authorization api that returns null body but with access token in headers.
I am able to read the okhttp3.Headers object and also get header names as Set using
Headers headers = response.headers(); // response object of type Response<T>
Set<String> headerNames = headers.names();
But in the code the headers object does not show the custom header (access_token) returned as response. However, in postman i can see the custom header as shown below:
access_token -> { "Token":"adklasldalksdalkdask",
"Provider":"ABC" }
I am using interceptors to get the header as shown:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
Can someone suggest how to read the access token as part of the custom header in auth response?
First print the entire response, body, code, message, header(by logging or something else) and try to find a clue from there.
I would recommend you to read the API docs and see the type of request it is asking for.
Use Postman to check which one of the following is working:
1.form-data
2.x-www-form-Urlencoded
3.raw
4.binary
And then accordingly set the annotations in the method declarations in the interface.
eg: in my case it was taking x-www-form-Urlencoded so I had to mention it using
#FormUrlEncoded
#Headers("Content-Type: application/x-www-form-urlencoded")
in the method declaration.

How to pass dynamic header authorization in Retrofit 2.0?

I want to pass dynamic header in retrofit using jwt,i received token using GET api and token has been saved using shared preference, i need to pass token as a header to receive user details when i login.Before that i have used volley library, in Retrofit just confusing please help me!
Put #Header on a method parameter and pass it as a value when invoking.
According to the docs:
// Replaces the header with the the value of its target.
#GET("/")
void foo(#Header("Authorization") String token, Callback<Response> cb);
Header parameters may be null which will omit them from the request. Passing a List or array will result in a header for each non-null item.
Note that HTTP headers do not overwrite each other. All headers with the same name will go in with the request. Then, it will be up to the server how they are interpreted. I recommend that you don't add multiple headers of the same name.

Categories

Resources