Error 401 from retrofit even after setting the Authorization Token

Trying to access an api with @POST and I already set my @Header("Authorization") String TOKEN.

I've tried it with @GET and it worked, but I'm passing a some form fields so I need to use @POST

@POST("details")
@FormUrlEncoded
Call<Play> playTrack(
        @Header("Authorization") String TOKEN,
        @Field("event_id") int event_id,
        @Field("longitude") double longitude,
        @Field("latitude") double latitude    
);

Try to create a header interceptor and add it to OkHttpClient :

Interceptor headerIntercepter  = new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {

      return chain.proceed(    chain.request().newBuilder().addHeader("authorization- 
client", accessToken).build());

        }
    };

OkHttpClient client = new OkHttpClient.Builder().connectTimeout(120, TimeUnit.SECONDS).readTimeout(120, TimeUnit.SECONDS)
            .addInterceptor(headerIntercepter)
            .build();

    try {

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

        apiService = retrofit.create(Api.class);

Advanced Retrofit2 (Part 2): Authorization Handling, Advanced Retrofit2 (Part 1): Network Error Handling & Response Use Case 2: App should be instantly logged out when token get Let's setup necessary classes first. 401 and 403 which conventionally denotes an authentication issue. An event is passed to it's listener (i.e: view/service) to take the� I've been following this guide while setting up my app. Everything works fine if I use password credentials flow and supply my own userame/password to get an access token. However, if I try to use client credentials flow, I get a 401 whenever I call any power bi endpoint. I also tried downloading the sample application provided here. Using "App

try using Multipart on annotation

@Multipart
@POST("details")
Call<ResponseBody> playTrack(
    @Header("Authorization") String token,
    @Part("event_id") RequestBody eventId,
    @Part("longitude") RequestBody longitude,
    @Part("latitude") RequestBody latitude,
);

make sure to pass a RequestBody as params

val latitude = RequestBody.create(MediaType.parse("text/plain"), doubleLatitude.toString())

Retrofit does not set Authorization in header � Issue #977 � square , So when authorize is called then intercept(RequestFacade request) IS called, but I'm getting 401 from the server. I'm able to fix it with setting� The 401 Unauthorized error is an HTTP status code that means the page you were trying to access cannot be loaded until you first log in with a valid user ID and password. If you've just logged in and received the 401 Unauthorized error, it means that the credentials you entered were invalid for some reason.

Seeing a 401 response means that the request was successfully executed and the server returned that status code. This is not a problem with Retrofit, but in whatever authentication information you are including in the request that the server expects.

Try with postman with the same data and check

N.B: Don't forgot to add Token Type as prefix to your token

Retrofit — Token Authentication on Android, We'll cover the topic of token authentication from an Android app to any If you read the previous tutorials about authentication with Retrofit, In case you're using another HTTP header field for your authentication token, either adjust even after passing the token it gives 401 error as response code , the� Token are temporary and get expired/refreshed after a certain period of time. Apps might need to get the updated token in some way so that access won’t be revoked. If expired, app need to take

Yet another OAuth 2.0 article for Android developers, Using OAuth 2.0 authentication in Android has become common Retrofit already has a way to do this, by bringing Authenticator to the return the request with 401 error since the refresh token failed 3 times. On SessionExpiration we clear the data and report the event. This is how our setup looks like:� Display text programmatically for TextView. I am looking to set a text programmatically, but for some reason, when I am doing so, no text appears on the screenI have attached my code below

Retrofit and auto relogin on a 401 response, After putting together a mighty scroll of java imbued with the power to throw authToken = headers.get(SET_COOKIE); return response.request(). cast itself anytime the evil api responds with 401 response code, so even� Ran the Javascript Implicit Client Demo and used the valid username and password (in memory service) to get the valid Id/ access token. Now, Clicked on the Call Service button and got 401 error, I noticed the Authorization token being passed in the request. I couldn't get a clue on the issue. Kindly help at the earliest.

OkHttp: How to Refresh Access Token Efficiently, When you use the token-based authentication including OAuth, there token is expired, there is an HTTP unauthorized error, with 401 error� The access token should be kept securely by the third party. Thanks for the heads-up notes, Jim! Because the access_token is uniquely generated by the Account holder (user / customer) at the time of SmartApp authorization (i.e., via a web page executing the steps you list), is there any vulnerability exposed if that Account holder (user / customer) is given this particular access_token?

Comments
  • What is your Authorization token type, is it bearer?
  • Error 401 means Unauthorized error. Check whether you are passing correct credentials for authorization
  • @Md.Asaduzzaman Yes
  • Then don't forgot to add Bearer as prefix to your token. "Bearer "+ token
  • I have tried this. It didn't work. But for my @GET annotation. it worked. so the issue is not the token.
  • yeah I know , I mean that u can add the header inside the interceptor like this return chain.proceed( chain.request().newBuilder().addHeader("Authorization", accessToken).build());
  • Yes I understand. I even tried that before posting here. Thanks
  • Yes. It was added