Sending data to Firebase from Matlab - matlab

I want to save data to firebase from Matlab. Does firebase have similar api calls like ThingSpeak? How can i send JSON data from matlab by making API calls?
I am making API calls from Matlab like for JSON:
Firebase_Url = 'https://ecgproject-86945.firebaseio.com/';
writeApiKey = '***';
data = ['api_key=',writeApiKey,'&name=',"JSOpn9ZC54A4P4RoqVa"];
response = webwrite(Firebase_Url,data)
%data = struct('api_key',writeApiKey,'field1',data); //also tries this
%options = weboptions('MediaType','application/json');
The Error:
Error using readContentFromWebService (line 46)
The server returned the status 405 with message "Method Not Allowed" in response to the
request to URL https://ecgproject-86945.firebaseio.com/.
Error in webwrite (line 139)
[varargout{1:nargout}] = readContentFromWebService(connection, options);
Error in Untitled (line 16)
response = webwrite(Firebase_Url,data)

From reading the mathworks documentation on webwrite you need to use the two-parameter version of the method, passing in the additional information inside the second, data object:
data = ['api_key=',writeApiKey,'&name=',"JSOpn9ZC54A4P4RoqVa"];
response = webwrite(FirebaseURL,data)

Okay I found the solution apparently i didn't add .json at the end of the URL. Thank You. Here is the solution:
Firebase_Url = 'https://***.firebaseio.com/Channel1.json';
response = webwrite(Firebase_Url,'{ "first": "Jack", "last": "Sparrow" }')

Related

Welcome Event not working using discordmongo & discordpy

so i have a cmd in a cog that suppose to set the welcome channel "welcomec" and from that its supposed to take that info and use it to post the message in the guild welcome channel but doesnt seem to work ngl can anyine help me
edited: after changing it to what RiVen told me i got a new error that I don't understand at all
main code:
welcome: event
#fright.event
async def on_member_join(member):
fright.db = fright.mongo['fright']
fright.welcome = discordmongo.Mongo(connection_url=fright.db, dbname="welcome")
dbguild = await fright.welcome.find({"_id": member.guild.id})
dbchannel = await fright.welcome.find({"welcomec": member.guild.id})
wguild = fright.get_guild(dbguild)
wchannl = await fright.fetch_channel(dbchannel)
await wchannl.send('worksss')
error:
raise HTTPException(r, data) discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body In channel_id: Value "None
NoneType error means that it doesn't correctly recognize the channel. When you use get_channel you are looking for the channel in the bot's cache which might not have your channel. You could use fetch_channel instead - which is an API call.
wchannl = await fright.fetch_channel(dbchannel)

Retrieving SendGrid Transactional Templates List

I have been trying to retrieve list of SendGrid transactional templates using API. I'm using correct API key and getting an empty array while there are about 5 transactional templates existing in my SendGrid account. Here is the response:
{
"templates": []
}
Any guesses what could be wrong?
Any guesses what could be wrong?
Yep, their documentation could be!
I also stuck with the problem and finally managed to solve it once I opened the devtools and saw how they request their own API from the UI. Long story short - one has to pass additional generations=dynamic query parameter. Here is the C# code I use:
var client = new SendGridClient("key");
var response = await client.RequestAsync(
SendGridClient.Method.GET,
urlPath: "/templates",
queryParams: "{\"generations\": \"dynamic\"}");
Using Api 7.3.0 PHP
require("../../sendgrid-php.php");
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
#Comma-delimited list specifying which generations of templates to return. Options are legacy, dynamic or legacy,dynamic
$query_params = json_decode('{"generations": "legacy,dynamic"}');
try {
#$response = $sg->client->templates()->get();
$response = $sg->client->templates()->get(null, $query_params);
echo $response->body();
exit;
} catch (Exception $e) {
echo '{"error":"Caught exception: '. $e->getMessage().'"}';
}
I had the same problem using the python wrapper provided by Sendgrid.
My code was similar to this:
response = SendGridAPIClient(<your api key>).client.templates.get({'generations': 'legacy,dynamic'})
This returned an empty array.
To fix you have to name the param or to pass None before the dict:
response = SendGridAPIClient(<your api key>).client.templates.get(None, {'generations': 'legacy,dynamic'})
or
response = SendGridAPIClient(<your api key>).client.templates.get(query_params={'generations': 'legacy,dynamic'})

Matlab webread with google translation api

I have the same problem when i called Google translation API, this is my code
url = 'https://translation.googleapis.com/language/translate/v2'
Options.ContentType ='Josn';
options.KeyValue='XXXXXX';
response = webread(url,options)
`options.KeyName = 'apikey';
the following error appeared :
Error using readContentFromWebService (line 37) The server returned the message: "Forbidden" for URL, 'https://translation.googleapis.com/language/translate/v2' (with HTTP response code 403).
Error in webread (line 115) [varargout{1:nargout}] = readContentFromWebService(connection, options);
I filled key value as it is in my console, any one help?

How to set the body of a http post request in Matlab

I'm trying to post to a restful service with Matlab. I've tried using webread, webwrite, and urlread and I cannot figure out how to set the body of the message.
My body is json and looks like this:
{"Item1": "string1", "Item2": "string2"}
I found out what I was doing wrong. I was constructing my body as a string literal and not as a matlab struct. Correct way:
api = 'http://myurl.net';
url = [api, '/Login'];
[un, pw] = GetAuthentication;
input = struct('Username',un,'Password',pw);
opts = weboptions('MediaType','application/json');
userInfo = webwrite(url, input, opts);

Azure encoding job via REST Fails

I am trying to upload a video and encode it via azure rest service.
I have now hit the step of encoding the video but I am having difficulties with the request.
The following code shows my request:
var joburl = res.RequestMessage.RequestUri + "Jobs";
client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Bearer " + token);
client.DefaultRequestHeaders.Add("x-ms-version", "2.8");
client.DefaultRequestHeaders.Add("DataServiceVersion", "3.0");
client.DefaultRequestHeaders.Add("MaxDataServiceVersion", "3.0");
client.DefaultRequestHeaders.Add("x-ms-date", date);
//accept
t = new NameValueHeaderValue("odata", "verbose");
type = new MediaTypeWithQualityHeaderValue("application/json");
type.Parameters.Add(t);
client.DefaultRequestHeaders.Accept.Add(type);
result = await client.PostAsync(joburl,json);
the url:https://wamsamsclus001rest-hs.cloudapp.net/api/Jobs
the json:
{"Name":"khgfiuydencodingjob","InputMediaAssets":[{"__metadata":{"Uri":"https://wamsamsclus001rest-hs.cloudapp.net/api/Assets('nb%3acid%3aUUID%3ad037b321-cd1c-43a9-9607-c4910fa7a85b')"}}],"Tasks":[{"Configuration":"H264 Adaptive Bitrate MP4 Set 720p","MediaProcessorId":"nb:mpid:UUID:1b1da727-93ae-4e46-a8a1-268828765609","TaskBody":"<?xml version=\"1.0\"encoding=\"utf-8\"?><taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset>JobOutputAsset(0)</outputAsset></taskBody>"}]}
The bearer token works as I use it for other request.
But I get a bad request 400 with the followin error message:
{"error":{"code":"","message":{"lang":"en-US","value":"Parsing request content failed due to: Make sure to only use property names that are defined by the type"}}}
Can anyone spot the error.
Thank you for the help
Okay I got it to work. Needed a odata=verbose in my json/string content - like this:
var jobInJson = JsonConvert.SerializeObject(job);
json = new StringContent(jobInJson, Encoding.UTF8);//,
json.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose");
I tried this earlier however I got a error 500 but now it is working.