androidtwilio-apiandroid-wireless

Getting Unauthorized response inspite of sending correct authorization details


I am using Twilio Rest API for sending wireless commands using the android studio. I have implemented the code structure according to their documentation for wireless commands. but when I hit the button to get the response, instead of getting the desired result I am getting Unauthorized 401 response. I am using their API for the first time, so clearly not having an idea, what I am doing wrong here.

Here is my code for this:

  public void byApi() {
    Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(ApiRetro.BASE_URL)
            .build();

here SIMSID is the unique anme for the twilio sim card and the command i am sending is hello machine! Credentials credentials=new Credentials(SIMSID,COMMAND);

    ApiRetro apiRetro = retrofit.create(ApiRetro.class);
    Call<ResponseBody> call = apiRetro.getSid(credentials);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if(response.isSuccessful())
            {
                try {
                    String hyyy=response.body().string();
                    try {
                        JSONObject jsonObject=new JSONObject(hyyy);

                        String test=jsonObject.getString("status");
                        Log.v("sdfg",test);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }


                try {
                    Log.v("TAG",response.body().string());
                    Toast.makeText(getApplicationContext(),response.body().string(),Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else
            {
                Toast.makeText(getApplicationContext(),response.code()+" "+response.message(),Toast.LENGTH_LONG).show();
                Log.v("one", String.valueOf(response.errorBody()));
                Log.v("two",response.message());
                Log.v("three", String.valueOf(response.code()));
                Log.v("four", String.valueOf(response.headers()));
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            t.getStackTrace();
        }
    });

}

The interface for the Retrofit request looks like this:

 String BASE_URL = "https://wireless.twilio.com/v1/";


@Headers({"AccountSid:yyyyyyyyyyyyyyyyyxxxx",
        "AuthToken: yyyyyyyyyyyyyyyyyyyyxxx",
        "token: ",
"Content-Type: application/x-www-form-urlencoded"})
@POST("Commands")
Call<ResponseBody> getSid(@Body Credentials credentials);

Body class:

public class Credentials {
 String Sim;

 String Command;

public Credentials(String sim, String command) {
    Sim = sim;
    Command = command;
}

public String getSim() {
    return Sim;
}

public void setSim(String sim) {
    Sim = sim;
}

public String getCommand() {
    return Command;
}

public void setCommand(String command) {
    Command = command;
}

}


Solution

  • OK ,i solved it .The issue was in the request where i was not sending according to the requirements.

    by adding interceptor solved my problem

     OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new BasicInterceptor(SID,AUTH))
                .build();
    

    and then just

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

    and removing the headers from the interface like

    @POST("Commands")
    @FormUrlEncoded
    Call<ResponseBody> getSid(@Field("Sim") String sim,@Field("Command") String command);