I have this codes to get access token:
Future<Null> getData() async {
var url = "http://192.168.1.23:7070/api/v2/token";
http.post(url, body:{
"grant_type": "string",
"branchcode": "string",
"password": "string",
"username": "string",
"dbname": "string",
"dbuser": "string",
"dbpassword": "string",
"dbtype": "string"
}).then((response){
print("Response Status: ${response.statusCode}");
print("Response Body: ${response.body}");
});
}
And after run this code block it returns something like this:
Response Body: {"access_token":"AQAAANCMnd8BFdERjHoAwE_Cl-sBAAAAgUTYyajkrEKWAWNrBvgpHQAAAAACAAAAAAAQZgAAAAEAACAAAAB-c92RaHBUfb0aNvnWFqx3JW29bBeQPIouZVDHsTsg8QAAAAAOgAAAAAIAACAAAAC7JdwAlRh8ckP3gAG4vc6H-nULfmjfpBF-gleCcFXeFzABAACJft4ZnsMt1clpRHUTAbiWQIqo5Zmt7_Qc7-pDPwA33O7z3VgFhkx2ITLn5xWGKcVHTkxOxPeRe4qrgEDbsd8V6DHcuwirPkAZye5zECUllxE6IDkNetLkTQsLuK2CpSWxAksFzUS6vhiU7fqkNKsXegtOV--0wFzWsq-ikTjYWnr4LsdAiSUfy_HsTMvLxoIjIqDSxw0QyMM1I2eVIE2wSsZSpoYn3CQIGejQAlG_mUIgzt3PBqEZ6kqIX-Qhx4jpcypUOG5GFNMKLUJa5mti1UiTi8ETpVN_8y_tDhVi-9AS2MJpJN7-Gao_fIB5s0yaH_m9fHkwFgXF1N-Y5GevDypdNryQWbHXQkf88DKZnljMSGzJkJhpzls3PTN8iwss32CXbeO1zaWm-iJAgMK_QAAAACVQGecH57e1QB-FRu2iCt-d6x37x6MJ-_H_H8uYOKbZYBX0lA1b8WxteCLWPM5mAJR1p3tHE_VBqQqvS4pKGHM","token_type":"bearer","expires_in":1199,"refresh_token":"f8de12c7a90b401b9400b93b4cf2c128"
My question is, how to retrieve the value of "access_token" in this code blocks?
You can access the response in the following ways
1.If it's from response headers you can access it by using
response.headers['access-token']
2.If it's from response body you can access it by using
print('access token is -> ${json.decode(response.body)['access_token']}');
3.If you are getting the object you can parse it and cast to required type by using this
final Map<String, dynamic> data = json.decode(response.body)['user'].cast<String, dynamic>();
Related
I am trying to send a standard/urgent message to my Viewsonic TV using its Open API
everything is working fine but when I try sending a message that is longer than 16 characters, I am getting an error message that says {message: 'Invalid Request Body'}.
I am realy confused as to why this is happening, it clearly says on the documentation that 300 characters is maximum for standard message.
Here is the sample snippet that I have
//this one can be converted using JSON.stringify()
const inputBody = '{
"deviceIds": [
"8jy9sne7-esbu-qrf7-d62f-c46srimnjcnn"
],
"entityId": "o39onp8a-um26-dgxf-v3iy-4xqqr2aqzw2h",
"message": "Hello world This is a really long message",
"type": "standard",
"siren": false,
"loops": 2
}';
const headers = {
'Content-Type':'application/json',
'Accept':'application/json',
'Authorization':'bearer ************'
};
fetch('https://oapi.myviewboard.com/devices/broadcast',
{
method: 'POST',
body: inputBody,
headers: headers
})
.then(function(res) {
return res.json();
}).then(function(body) {
console.log(body);
});
Also, I noticed on the sample code from the documentation, what does this symbol mean u)s]? Maybe it has something to do with this issue.
const inputBody = '{
"deviceIds": [
"8jy9sne7-esbu-qrf7-d62f-c46srimnjcnn",
"jb9a2hpd-h2qc-hp9m-4i36-2m5wzqmn7o9w"
],
"entityId": "o39onp8a-um26-dgxf-v3iy-4xqqr2aqzw2h",
"message": "u)s]",
"type": "standard",
"siren": true,
"loops": 84479723
}';
I want to send a put request to modify the command part of my JSON file. Here is my JSON;
{
"users": [
{
"hostname": "xxx.xxx.xxx.xx",
"password": "password1",
"command": "command1",
"id": 1
},
{
"hostname": "xxx.xxx.xxx.xx",
"password": "password2",
"command": "command2",
"id": 2
},
{
"hostname": "xxx.xx.xx.xxx",
"password": "password3",
"command": "command3",
"id": 3
}
]
}
In App.js I send put request like this;
stopPC(id){
axios.put('http://localhost:3002/users/'+id,{
command: 'stop'
})
}
And I have tried this;
axios({
method: 'put',
url: 'http://localhost:3002/users/'+ id,
data: {
hostname: id.hostname,
password: id.password,
command: 'stop'
}
});
In both, I got the following output in the JSON file.
{
"users": [
{
"command": "stop",
"id": 1
},
{
"hostname": "xxx.xxx.xxx.xx",
"password": "password2",
"command": "command2",
"id": 2
},
{
"hostname": "xxx.xxx.xxx.xx",
"password": "password3",
"command": "command3",
"id": 3
}
]
}
I want to change only the command information while keeping the hostname and password information the same.
I'm not sure where I went wrong, I'd be glad if you could help.
If id is of type Number (which I think it is), id.hostname and id.password would be undefined, so basically
{
hostname: id.hostname,
password: id.password,
command: 'stop'
}
and
{
command: 'stop'
}
are the same to Javascript and you're effectively sending the same payload. I think that the best way of solving this would be changing how the backend handles the incoming payload by just changing the properties that came in the request. If you don't have access to changing the backend, you would have to get or read the current stored value and use that in the payload. Something like
user = axios.get('http://localhost:3002/users/'+id).then(r => r.data)
axios.put('http://localhost:3002/users/'+id,{
hostname: user.hostname,
password: user.password,
command: 'stop'
})
Also, PUT is meant to be used used to replace a record, and PATCH to update the existing one, which sounds more like what you want to do. Maybe just try axios.patch and it may work.
I did quite the same than #rodrigo-naranjo except I embedded the put call in the response of the get call:👇
axios.get("http://localhost:3002/users/" + id).then((r) => {
let user = r.data;
axios.put("http://localhost:3002/users/" + id, {
hostname: user.hostname,
password: user.password,
command: "stop",
});
});
with id an input value from your app context.
According to the react-native-fcm package you can include a custom nested object within the data object for a FCM messaging payload.
according to this post by the package author
Like this:
var payload = {
data: {
custom_notification: {
title: 'title',
body: 'body',
priority: 'high',
id: 'id',
group: 'group'
}
}
};
This is for the purpose of receiving heads up notifications in all app states, which will not happen if you do just a notification payload or data payload.
When I implement this in my cloud function I get the following error:
Error: Messaging payload contains an invalid value for the "data.custom_notification" property. Values must be strings.
So I'm at a loss as to how others can be using this successfully?
I wonder if there's some issue going on with my environment or something as the following test payload which was given to me by firebase support (and is in the docs) errors out:
var payload = {
"to":"FCM_TOKEN",
"data": {
"type":"MEASURE_CHANGE",
"body": "test body",
"title": "test title",
"color":"#00ACD4",
"priority":"high",
"id": "id",
"show_in_foreground": true
}
};
I get the following error:
Error sending message stringify: {"code":"messaging/invalid-payload","message":"Messaging payload contains an invalid \"to\" property. Valid properties are \"data\" and \"notification\"."}
Been at this for days so hopefully I can get a bit of help on this.
Thanks in advance!
So I realised just now (after days of searching) that the package react-native-fcm is using a different send method than admin.messaging().sendToDevice(token, payload, options). I had been using that for a while now and didn't realize that it was not actually what was intended to be used with this library or atleast in this scenario. Mainly because everything was working fairly well using admin.messaging() up until I wanted heads up notifications in all app states.
The other method is like this
sendData(token) {
let body = {
"to": token,
"data":{
"title": "Simple FCM Client",
"body": "This is a notification with only DATA.",
"sound": "default",
"click_action": "fcm.ACTION.HELLO",
"remote": true
},
"priority": "normal"
}
this._send(JSON.stringify(body), "data");
}
_send(body, type) {
let headers = new Headers({
"Content-Type": "application/json",
"Content-Length": parseInt(body.length),
"Authorization": "key=" + FirebaseConstants.KEY
});
fetch(API_URL, { method: "POST", headers, body })
.then(response => console.log("Send " + type + " response", response))
.catch(error => console.log("Error sending " + type, error));
}
You can use nested objects within the data object using this method. The documentation is not super clear on this unfortunately, I didn't even realise there was an example until now. Of course this could have just been me.
When using the data message payload, it is stated to use key value pairs that are String, so what you could do is have the value of your custom_notification as JSON String by enclosing them in " ".
For the sample payload provided, are you actually using the FCM_TOKEN in the to parameter? You're supposed to replace it with an actual token.
When I try to create an envelope from a template I get a response of:
{ errorCode: 'UNSPECIFIED_ERROR',
message: 'Non-static method requires a target.' }
Here's what I'm doing so far:
First I login, which returns
{ loginAccounts:
[ { name: '*****',
accountId: '*****',
baseUrl: 'https://demo.docusign.net/restapi/v2/accounts/******',
isDefault: 'true',
userName: '***** ********',
userId: '*******-*****-*****-*****-*********',
email: '********#*******.com',
siteDescription: '' } ] }
So then I take the baseUrl out of that response and I attempt to create the envelope. I'm using the hapi framework and async.waterfall of the async library, so for anyone unfamiliar with either of these my use of the async library uses the next callback to call the next function which in this case would be to get the url for the iframe, and with our usage of the hapi framework AppServer.Wreck is roughy equivalent to request:
function prepareEnvelope(baseUrl, next) {
var createEntitlementTemplateId = "99C44F50-2C97-4074-896B-2454969CAEF7";
var getEnvelopeUrl = baseUrl + "/envelopes";
var options = {
headers: {
"X-DocuSign-Authentication": JSON.stringify(authHeader),
"Content-Type": "application/json",
"Accept": "application/json",
"Content-Disposition": "form-data"
},
body : JSON.stringify({
status: "sent",
emailSubject: "Test email subject",
emailBlurb: "My email blurb",
templateId: createEntitlementTemplateId,
templateRoles: [
{
email: "anemailaddress#gmail.com",
name: "Recipient Name",
roleName: "Signer1",
clientUserId: "1099", // TODO: replace with the user's id
tabs : {
textTabs : [
{
tabLabel : "acct_nmbr",
value : "123456"
},
{
tabLabel : "hm_phn_nmbr",
value : "8005882300"
},
{
tabLabel : "nm",
value : "Mr Foo Bar"
}
]
}
}
]
})
};
console.log("--------> options: ", options); // REMOVE THIS ====
AppServer.Wreck.post(getEnvelopeUrl, options, function(err, res, body) {
console.log("Request Envelope Result: \r\n", JSON.parse(body));
next(null, body, baseUrl);
});
}
And what I get back is:
{ errorCode: 'UNSPECIFIED_ERROR',
message: 'Non-static method requires a target.' }
From a little googling it look like 'Non-static method requires a target.' is a C# error and doesn't really give me much indication of what part of my configuration object is wrong.
I've tried a simpler version of this call stripping out all of the tabs and clientUserId and I get the same response.
I created my template on the Docusign website and I haven't ruled out that something is set up incorrectly there. I created a template, confirmed that Docusign noticed the named form fields, and created a 'placeholder' templateRole.
Here's the templateRole placeholder:
Here's one of the named fields that I want to populate and corresponding data label:
As a side note, I was able to get the basic vanilla example working without named fields nor using a template using the docusign node package just fine but I didn't see any way to use tabs with named form fields with the library and decided that I'd rather have more fine-grained control over what I'm doing anyway and so I opted for just hitting the APIs.
Surprisingly when I search SO for the errorCode and message I'm getting I could only find one post without a resolution :/
Of course any help will be greatly appreciated. Please don't hesitate to let me know if you need any additional information.
Once I received feedback from Docusign that my api call had an empty body it didn't take but a couple minutes for me to realize that the issue was my options object containing a body property rather than a payload property, as is done in the hapi framework.
I am trying to create a user's feed on my page using Facebook JS SDK but I am having issues accessing the values of nested objects.
This is my code:
FB.api('/me/feed', function(response) {
alert(response.data);
});
The alert here will return [object Object]
The data object is this:
{
"data": [
{
"message": "Some message",
"id": "some ID",
"created_time": "2014-01-05T22:46:10+0000"
}
],
"paging": {
"previous": "Link",
"next": "Link"
}
}
I need to access the message part of the object but I don't know how.
Simply access the properties directly
FB.api('/me/feed', function(response) {
alert(response.data.message);
});
I had to use JSON after all
FB.api('/me/feed',{limit:10}, function(response){
var jsonText = JSON.stringify(response.data,["picture"]);
alert(jsonText);
});
This will return an array with all the values of 'picture'
This is the example output
[{"picture":"https://fbcdn-photos-d-a.akamaihd.net"},
{"picture":"https://fbcdn-photos-g-a.akamaihd.net"},
{"picture":"https://fbexternal-a.akamaihd.net"},{},
{"picture":"https://fbcdn-profile-a.akamaihd.net"}]
It looks likedata is an array with a single element, so to get message, you will want to use response.data[0].message