In one of my projects I need to write data from an app (iOS and Android) to a Google Sheet document. I need to write the data to a spreadsheet that is not belonged to the user, the spread sheet is under my company account.
According to the docs, for this type of scenario I can use Create credentials > API key to set up a none-authentication key.
My concerns are that anyone could fetch this key within the app (using some basic reverse-engineering methods). I had an idea to create a user with write only credentials and using his API-KEY to preform the API requests. But i'm sure there is a better way to achieve this.
What are my options? Thank you so much!
you can use google app scripts to depoly a webapp that can be accessed by anyone even anonymous and then create an Async method in your app that runs that script. an example of such a webapp can be seen here: https://gist.github.com/petyr47/7af08ec2d982399c1b4ace2734597d3b.js then you can add an AsyncTask method that looks like this :
public class SendRequest extends AsyncTask<String, Void, String> {
protected void onPreExecute(){}
protected String doInBackground(String... arg0) {
try{
//Enter script URL Here
URL url = new URL("Your App Script Web App URL");
JSONObject postDataParams = new JSONObject();
//Passing scanned code as parameter
postDataParams.put("sdata",scannedData);
Log.e("params",postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
else {
return new String("false : "+responseCode);
}
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
}
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
}
you can execute this method with:
new SendRequest().execute();
replace the parameters in the method with what you would like to send. hope this helps
this is a solution for android though, i can't help with iOS but you can apply the same principle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With