Setting the oauth "Authorization"header in Java - Android - android

I'm trying to set the access token with all of my network calls in my Android app.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "Token token="
+ accessToken);
conn.setRequestMethod("GET");
With this code, however, the header that our server is seeing is "HTTP_AUTHORIZATION" - not "AUTHORIZATION". After pouring over javadocs, I can't figure out how to not get it to append the "HTTP_".
Thanks!

You can't. The mapping of Authorization to HTTP_AUTHORIZATION is part of the CGI Specification and happens on the server.

Related

The App is showing warnings related to HttpClient and HttpGet()

Warnings for HttpClient,HttpGet().
The code is running but it is not getting any response from the Esp8266(I have used Esp8266 as a server in my application).
Is it related to the warnings of HttpClient or HttpGet() as shown in the image or related to the Esp8266 coding?
Please guide!
HTTPClient is deprecated since API 22. You can try using HttpURLConnection instead.
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("User-Agent", "");
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();
More info in this answer : https://stackoverflow.com/a/29889139/5040796

How to go for refresh token automatically when access token expired and get 401 using HttpURLConnection in android?

I have post request like below, Cant we make it automatic like Retrofit 2.0 interceptor if access token expires get refresh token and service call continues? without interruption?
URL url = new URL(myurl + "?access_token=" + access_token);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(100000000 /* milliseconds */);
conn.setConnectTimeout(150000000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("charset", "utf-8");
conn.setDoInput(true);
conn.connect();
Is there any method to do this automatically when we get 401 response code ?
As far as i know, No way is provided for this in any framework as this is purely requirement based. You will have to post SERIAL calls as stated below :
Post Main Call with Access Token; Iff successful than Stop Else go to Step2
Post Call for New Access Token with Refresh Token, If Successful Update access token and again Post Main call like step 1 with updated Access token.
Same way if your refresh token can expire, than handle that in same SERIAL Way.
Hope it helps a bit.

Get JSON from an HTTP Post to an Algolia server on Android

Not sure how to correctly put together the headers for a POST query to an Algolia server using Java in Android.
I have all the info about the request headers from recording the network info in Chrome's dev console
I have the query string parameters:
x-algolia-api-key
x-algolia-application-id
x-algolia-agent
and the form data params
query
hitsPerPage
facets
not working Android Code:
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("accept", "application/json");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("query", queryString);
con.setRequestProperty("hitsPerPage", "20");
con.setRequestProperty("facets", "*");
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
This keeps returning a 404 error but it's likely not set up right, really new to network connectivity on Android, anything helps!
I personally used the Algolia PHP API client (on github) in order to perform the search queries, which helps me to don't have to worry about the REST spec.
That one makes things easier to integrate to an Android app, but also integrates a retry mechanism that improves the connection reliability between the device and the API in case of network issue.
The same search query looks like that:
APIClient client = new APIClient("YourApplicationID", "YourAPIKey");
Index index = client.initIndex("YourIndexName");
index.searchASync(new Query(queryString), this)
.setFacets("*", this)
.setNbHitsPerPage(20), this);
You should never do a direct request to Algolia's servers, as you would lose all the logic and optimizations they put in place in their API Clients.
Using the Android API Client, doing a search query is simple:
Index index = new APIClient("YourApplicationID", "YourAPIKey")
.initIndex("YourIndexName");
JSONObject result = index.search(new Query(queryString)
.setFacets("*").setHitsPerPage(20));
But have a look at InstantSearch Android, Algolia's library to build search interfaces. It will be easier, faster, and safer to use it than the API Client.

GCM Diagnostics shows messages only for production package name

How can I have GCM diagnostic information for a develop version of an app?
The GCM Diagnostics page, under https://play.google.com/apps/publish/, correctly shows message information when I send messages to the main package, but not when .debug is added as a suffix. I can successfully send messages to either app using the same API key and the appropriate Registration ID. For reference here is my test message sending code, straight from the quickstart example.
// Create connection to send GCM Message request.
URL url = new URL("https://android.googleapis.com/gcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "key=" + API_KEY);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
// Send GCM message content.
OutputStream outputStream = conn.getOutputStream();
outputStream.write(jGcmData.toString().getBytes());
Interestingly both of the Registration IDs end up being mapped together and the dashboard returns only the one message sent to the the app with the production package name.

How to connect a full web site rather than mobile programmatically from android applicaiton

How can I connect to a full web site rather than mobile web page from code? The reason why I ask this is when I try to connect a web site which has a mobile web site, it directs to mobile page so I can't get the content of the full site.
I'm using this code to connect to site but I tried two or three different code to connect all of them didn't work.
URL url = new URL(adress);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
Thanks
try to set proper header properties e.g. like this:
conn.setDoOutput(true);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
conn.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0) Gecko/20100101 Firefox/5.0");
conn.setRequestProperty("Accept-Language", "en-gb,en;q=0.5");
conn.setRequestProperty("Referer", "http://youtube.com");
conn.setRequestProperty("Accept-Encoding", "deflate");

Categories

Resources