Can't get OAuth to work, so I settled for a public spreadsheet, able to download data via URL (range/value). Struggling to get upload data via JSON embedded in POST, then I thought I saw some other approach: Is it somehow possible to write an app script that writes a value to cell and embed that data in the URL together with the request to execute the app script ?
Basically, download data via GET and upload data via GET, sounds crazy ?
Sure. You just need a function called doGet and then you can deploy the script as a web app. Here is an example.
function doGet(e) {
return ContentService.createTextOutput(JSON.stringify(e, null, 2));
}
If you then open the web app url with some parameters added to it e.g. https://script.google.com/macros/s/.../exec?a=3&b=4 you should see that those parameters are now contained in e so your script can just read them and add them to your spreadsheet.
Related
I have a Spreadsheet with some Apps Script functions bound to it.
I also have an Android client using Google Sheets API v4 to interact with that spreadsheet.
Is there a way for the Android client to call/run some function in the Apps Script code?
The reason I need to code to be run on the Apps Script side, and not simply on the Android client, is because I'm sending some email when something happens to the doc, and I would like the email to be sent from the owner account of the spreadsheet, and not from the Android user authenticated via the API.
I know I can trigger functions implicitly like by adding rows to a doc, but is there a way to directly run a specific function?
Yes. You can make GET and POST requests to Google apps-scripts. from anywhere that can make REST type calls including clients. If you need authentication there is also the apps-script client libraries. I wrote a short script for emailing from a request from one apps-script to another here. But, it would work if you called the emailing script from your client also.
Deploy your Google Apps Script as Web Apps > reference, by this way you can run function Get(e) or Post(e) and invoke other functions inside one of them with conditions....
You might have gotten the answer to your question. Just in case you have not, below are some points that may help with your development:
1) Create the server side script (i.e., Google Apps Script) function like usual:
function myFunction(inputVar) {
// do something
return returnVar;
}
2) Create a doGet(e) or doPost(e) function like below - can be in the same .gs file with the function in 1) :
function doGet(e) {
var returnVar = "";
if (e.parameter.par1 != null) {
var inputVar = e.parameter.par1;
returnVar = myFunction(inputVar);
}
return HtmlService.createHtmlOutput(returnVar);
}
3) Publish and deploy your project as webapp. Note the deployed URL.
4) From your Android client do HTTP call with the URL as: your_webapp_url?par1="input value"
I am developing a mobile app on Android.
I download a lot of information from the backend via REST API.
For example for obtain the information about a contract I use the api the following request:
GET /contracts/01212314.json
It return a json with many fields
{
"conto_contrattuale": "01212314",
"intestatario": "Dennis D'Amico",
"utilizzo": "COTTURA + PROD. ACQUA 7 GG",
"codice_settore_merceologico": "E1",
"settore_merceologico": "ELETTRICITA",
"codice_societa_vendita": "Z016",
"societa_vendita": "Estra Energie S.r.l.",
"fornitura_indirizzo": "Via Palermo",
"fornitura_civico": "20",
"fornitura_precisazione": "Rosso",
"fornitura_cap": "59100",
"fornitura_comune": "Prato"
"rid": false,
"fatt_elettronica": true,
"fatt_email": "andrea.bettarini#devise.it",
"fatture_scadute": 1,
}
But I am only interested to the field : "fornitura_indirizzo"
I can't modify the backend and the API.
So I think to create a new middle backend that fetch the info from the actual backend and exposes a call only for the field "fornitura_indirizzo".
How can I do?
Is it possible on google cloud platform? and is it free?
Thank you for your consideration.
You can put a file on your server that fetches the data en then recreates a partial array and echo that in json.
Example:
$contract = Json_decode("/contracts/xxxx.json", true);
$needed_info = $contract["fornitura_indirizzo"];
Echo json_encode($needed_info);
I can't access to the server and modify. I can only send request.
I apologise if I misunderstand your goals here, but why bother with creating this new back end for your app. You could simply call the existing back end and ignore the unneeded information. Either way, that existing back end has to send all that info somewhere for every request your mobile app performs. You're also adding more latency to the request since it has to go through your back end first.
I can see this being worth it though if your goal is to minimize the data downloaded by the mobile app when it does a request.
I've stored my .apk file in my rails server. I've set up a route that redirects a given url to a method that essentially sends the file
in my routes.rb file
match '/myApk.apk' to: 'upgradeapk#index'
Upgradeapk_controller.rb file
def index
#filename = '/myApk/myApk.apk'
#tmpfile = 'upgradedApk.apk'
send_file(#filename, :disposition => 'inline', :stream => true, :type=> 'application/vnd.android.package-archive', :file_name => #tmpfile)
end
When i type my sever url and add '/myApk.apk' it starts the downloading process as long as i do it on my a computer. However if i try to do it on my android device it doesn't work. Checking the download lists in my android device browser i notice that the download "job" for the apk is created, however its in an endless loop changing between states "in queue" and "downloading". Nothing ever downloads.
Do I have to set the send_file differently when it comes to making it work on android devices?
----EDIT------
Ok so i've decided to store the files in a dropbox location instead of storing it in my server. If i pass the url for the file directly in my android function for the http request, it works well. The file is found, downloaded and the installation is prompted.
class UpgradeapkController < ApplicationController
def index
android_apk = Androidaplicacion.first (Model to access the table in which i store the apk dropbox url)
route = android_apk.url
redirect_to route
end
end
I've set up my controller to redirect to the given url for the dropbox file. If i try the url (same as before, using the "match" url) in my android browser, this time it downloads. However, if I try it from the android app, its the same as before, it just doesnt download.
So, the "send_file" method seems to not be working if its on the android platform. redirecting to my dropbox url from the controller works on android but only from a browser, not using the http request on my android app. The only way to get it to work in my android app is if I use the dropbox direct link.
Also, I first thought this was because my server was running on https and the certificate its not a valid one. I found a way to bypass the https certification encryption/certificate validation thing on my android app but it didnt work either (it appeared to have succeed in avoiding the validation) but the results ended up been the same. I then ran an instance of my server using http and still same results.
I am trying to develop a Moodle Android app. I am using MoodleREST source code for my reference. But rest code to upload assignment is not provided by this library. I want to be able to upload assignment from mobile client with a webservice call. Uploading assignment using a webview is possible but in that case user need to login again to access upload assignment page.
I have found something similar here https://moodle.org/mod/forum/discuss.php?d=207875.
I am new to moodle and still learning it, so my question can be a little naive so please bear with it :)
It is kind of possible to upload a submission with a file to the assignment using Moodle webservices.
First upload a file to draft using core_files_upload
http://my-moodle-url/moodle/webservice/rest/server.php?wstoken=token_value_xyz&moodlewsrestformat=json&wsfunction=core_files_upload&component=user&filearea=draft&itemid=0&filepath=/&filename=test2.txt&filecontent=TWFuIGlzIGRpc3Rpbmd1aXNoZWQ=&contextlevel=user&instanceid=8
where:
itemid=0 - moodle will generate and return an itemid or you set itemid
filecontent - base64 encoded file contents
instanceid - userId whose is webservices token
Sample response:
{
"contextid": 26,
"component": "user",
"filearea": "draft",
"itemid": 293005570,
"filepath": "/",
"filename": "test3.txt",
"url": "http://my-moodle-url/moodle/draftfile.php/26/user/draft/293005570/test3.txt"
}
You can search for an assignment id for the next call with mod_assign_get_assignments
Then use itemid received, here "293005570", in mod_assign_save_submission
http://my-moodle-url/moodle/webservice/rest/server.php?wstoken=token_value_xyz&moodlewsrestformat=json&wsfunction=mod_assign_save_submission&assignmentid=5&plugindata[onlinetext_editor][text]
=some_text_here&plugindata[onlinetext_editor][format]
=1&plugindata[onlinetext_editor][itemid]=521767865&plugindata[files_filemanager]=521767865
This will add an assignment submission with this file.
The problem I could core_files_upload and mod_assign_save_submission only using a webservices token for a particular user, i.e. each user needs a webservices token which might be not practical. With a webservices user token I get on the first call:
{
"exception": "moodle_exception",
"errorcode": "nofile",
"message": "File not specified"
}
Tested with Postman. This might be related: https://tracker.moodle.org/browse/MDL-61276
Doesnt look like there is existing solution for this in moodle web services. Moodle actually encodes files in base64 which creates burden on mobile devices. Mobile devices dont have that much memory to encode big files.
Closet solution published by Moodle HQ (and otherwise) is this : https://github.com/moodlehq/sample-ws-clients/blob/master/PHP-HTTP-filehandling/client.php which saves file as private file and not as assignment. You may have to modify substantially the plugin.
To upload files I'm using this API with a POST method
https://{YOUR_URL}/webservice/upload.php?moodlewsrestformat=json&wstoken={WSTOKEN}
And you must pass the following parameters as FormData
file => File // your file
token => Int // same user's wstoken
filearea => String // draft, private... etc
itemid => Int // set to 0 to create a new file
I am wondering what is required to setup a server so that you can store data on it, and then have an application send requests to it to store and receive data. More specifically, I am working on an Android application where a user will generate data and then that should be stored on a server so other users can access it. But I do not know how setting up a server to be capable of this works. I have worked on Android applications in the past that sends requests (put, post, get, etc) to a server, but that back end was already set up for me. Any info or resources about setting this up would be great.
There are many, many different ways to accomplish this.
Since you're already working with a Google technology, Android - you could start by creating a Google App Engine project. Following the tutorials you can get started setting up a simple back end solution that will store data for you and you can make requests to it for that data.
Another advantage to this for you is that you don't have to learn how to install software on a server and all the dependencies that arise from that, etc. Simply set up a new account and push-button deploy through Eclipse or command line.
And since you've used Java in Android, you can use JAva for Google App Engine (GAE) too!
Getting started: http://code.google.com/appengine/docs/java/gettingstarted/introduction.html
You can try ready to use BAAS/PAAS services to store your data, e.g. QuickBlox for Android http://quickblox.com/developers/Android, where you can manipulate with your data with few strings
QBLocation location = new QBLocation();
location.setLatitude(35.0);
location.setLongitude(53.0);
location.setStatus("I'm at this place");
String someImportantString = "Dr. Henry Walton Indiana Jones";
QBLocations.createLocation(location, new QBCallbackImpl() {
#Override
public void onComplete(Result result, Object context) {
// retrieve context object inside callback
String passedContextObject = (String) context;
System.out.println(passedContextObject);
// do stuff with result
}
}, someImportantString);
All logic of data exchange with server is encapsulated in framework.