Jump to content









Photo

android HttpURLConnection apikey

401 apikey

  • Please log in to reply
2 replies to this topic

#1 shrekware

shrekware

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 20 April 2018 - 02:37 AM

I have been trying to retrieve data from Carriots using android htttpURLconnection, but adding the apikey header is not working for me, returns 401.  I have tried to add the apikey several different ways and the connection always returns a 401 not authorized error. the code is below and the response messages are below the code.  I really just need to fix the .addRequestProperty for the API key or so I think. 
 

    public static String getJSONObjectFromURL(String urlString) throws IOException, JSONException {

        HttpURLConnection urlConnection = null;
        URL url = new URL(urlString);
try{
        urlConnection = (HttpURLConnection) url.openConnection();
       urlConnection.addRequestProperty("Host","api.carriots.com");
       urlConnection.addRequestProperty("Authorization:carriots.apiKey:",CARRIOTS_API_KEY);
       Log.v("FetchSensorData", urlConnection.getHeaderField(1));

        urlConnection.setRequestMethod("GET");
        urlConnection.setReadTimeout(10000 /* milliseconds */);
        urlConnection.setConnectTimeout(15000 /* milliseconds */);
        urlConnection.connect();}
        catch (IOException error){
    Log.v("IOEXCEPTION ERROR", error.toString());
    
        }

04-19 22:26:18.486 3393-3842/

V/FetchSensorData - error: buffer(com.android.okhttp.internal.http.HttpConnection$ChunkedSource@cae91fc).inputStream()

 

04-19 22:26:18.486 3393-3842/

V/FetchSensorData-header: {null=[HTTP/1.1 401 Unauthorized], Access-Control-Allow-Credentials=[true], Access-Control-Allow-Origin=, Access-Control-Expose-Headers=[Date,Server,Allow,Connection,Content-Length,Content-Type,Cache-Control], Allow=[GET], Cache-Control=[no-cache], Connection=[keep-alive], Content-Type=[application/vnd.carriots.v2+json; charset=utf-8], Date=[Fri, 20 Apr 2018 02:26:18 GMT], Server=[Carriots REST API], Transfer-Encoding=[chunked], X-Android-Received-Millis=[1524191178486], X-Android-Response-Source=[NETWORK 401], X-Android-Sent-Millis=[1524191178328]}

 

04-19 22:26:18.486 3393-3842/  V/FetchSensorData - code: 401 code



#2 sdeancos

sdeancos

    Master of Puppets

  • Members
  • PipPipPip
  • 107 posts
  • LocationMadrid, Spain.

Posted 20 April 2018 - 07:48 AM

Hi!

This line:
urlConnection.addRequestProperty("Authorization:carriots.apiKey:",CARRIOTS_API_KEY);


Should be:
urlConnection.addRequestProperty("Carriots.apikey:",CARRIOTS_API_KEY);


BR


#3 shrekware

shrekware

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 20 April 2018 - 12:42 PM

thanks BR, You are very close, no colon after apikey, when i tried it with the colon at the end of the apikey, the wireshark capture shows 2 colons in the request, and still didnt work, 401 error, the api was in the header. I have posted the fix below, it seems the apikey was being sent, but the json object still has issues displaying the data 

 

Hypertext Transfer Protocol
    GET /time_zones/ HTTP/1.1\r\n
    carriots.apiKey:: the APIKEY was here removed for privacy\r\n
    User-Agent: Dalvik/2.1.0 (Linux; U; Android 7.1.1; Android SDK built for arm64 Build/NYC)\r\n
    Host: api.carriots.com\r\n
    Connection: Keep-Alive\r\n
    Accept-Encoding: gzip\r\n
    \r\n
    [Full request URI: http://api.carriots.com/time_zones/]
    [HTTP request 1/1]
    [Response in frame: 152161]
 
This worked, at least I received a 200 OK on this request, 
 
    public static String getJSONObjectFromURL(String urlString) throws IOException, JSONException {

        HttpURLConnection urlConnection = null;
        URL url = new URL(urlString);
    try{
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.addRequestProperty("Carriots.apikey",CARRIOTS_API_KEY);
        Log.v("FetchSensorData", urlConnection.getHeaderField(1));
        urlConnection.setReadTimeout(10000 /* milliseconds */);
        urlConnection.setConnectTimeout(15000 /* milliseconds */);
        urlConnection.setRequestMethod("GET");

         urlConnection.connect();}
        catch (IOException error){
    Log.v("IOEXCEPTION ERROR", error.toString());

        }

 






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users