I am configuring my mobile applications with firebase cloud messaging.
I've finally figured out how to send these annoying to configure notifications.
My python code looks like this
url = 'https://fcm.googleapis.com/fcm/send'
body = {
"data":{
"title":"mytitle",
"body":"mybody",
"url":"myurl"
},
"notification":{
"title":"My web app name",
"body":"message",
"content_available": "true"
},
"to":"device_id_here"
}
headers = {"Content-Type":"application/json",
"Authorization": "key=api_key_here"}
requests.post(url, data=json.dumps(body), headers=headers)
I would think that putting this in a for loop and swapping device ids to send thousands of notifications would be an immense strain on the server and a bad programming practice. (Correct me if i'm wrong)
now the documentation tells me to create "device groups" https://firebase.google.com/docs/cloud-messaging/notifications which store device_id's to send in bulk....this is annoying and inefficient. As my groups for my web application are constantly changing.
Plain and Simple
How do I send the notification above to an array of device id's that I specify in my python code so that i can make only 1 post to FCM instead of thousands.
To send FCM to multiple device you use the key "registration_ids" instead of "to"
"registration_ids": ["fcm_token1", "fcm_token2"]
Have a look at this package and see how they implemented it.
Instead of "to":"device_id" you should use "to":"topic" ,
topic is use from group messaging in FCM or GCM
https://developers.google.com/cloud-messaging/topic-messaging
Related
I'm developing app that is going to be run on a headless server. To launch it I need to possess access and refresh tokens that is done by following request https://developers.upwork.com/?lang=python#authentication_access-token-request. I'm using python, so my request looks like:
import upwork
config = upwork.Config(
{
"client_id": <my_client_id>,
"client_secret": <my_client_secret>,
"redirect_uri": <my_redirect_uri>
}
)
client = upwork.Client(config)
try:
config.token
except AttributeError:
authorization_url, state = client.get_authorization_url()
# cover "state" flow if needed
authz_code = input(
"Please enter the full callback URL you get "
"following this link:\n{0}\n\n> ".format(authorization_url)
)
print("Retrieving access and refresh tokens.... ")
token = client.get_access_token(authz_code)
As a result token object looks like:
{
"access_token": <access_token>,
"refresh_token": <refresh_token>,
"token_type": "Bearer",
"expires_in": 86400
}
Given access_token and refresh_token I put them to my program and it is successfully launched. To keep continuous access to Upwork API I need to have valid access_token which expires every 24 hours, so I renew it with refresh_token. But the problem is than last one's lifespan is 2 weeks and when it's gone I can't use it to refresh access token, so need to get new one. In the documentation I haven't found how to do so and it seems that the only way is to go through the whole process of obtaining tokens pair again as described above, but that's not an option for me because as I said I want to deploy an application on a headless server without ability to redirect user. I need the way to get tokens pair every 2 weeks without manual intervention
Expecting:
Find a way to refresh refresh_token without redirecting user and manual intervention at all
you can set a timer, that is going to call refresh-token a moment before it expires. This is one way to do it. But maybe someone will come up with a better idea. I've seen people doing this with access token, which wasn't a good practice in that case. But you have a different situation.
#sviddo, if there is no activity for 2 weeks, the authentication is required, involving the user manual login. It's a security requirement.
The other thing is that a refresh token is valid for 14 days, and its TTL automatically extended when refresh is performed. If it's not the case, please, contact Support Team at Upwork
I’m trying to send a photo saved on my pc through the SkyBiometry API but it says error_message: MISSING_ARGUMENTS - access_token is missing and I don’t know how to fix it.
This is the code I used:
auth_headers = {
‘api_key’: ‘my_api_key’,
‘api_secret’: ‘my_api_secret’,
‘Content-Type’: ‘application/json’
}
url = ‘http://api.skybiometry.com/fc/faces/detect’
files = {
‘source’: open(r"C:\Users\Diego\OneDrive\Desktop\API\img\happy.png", “rb”)
}
data = {
‘timeout’: 60
}
response = requests.post(url, files=files, data=data, headers=auth_headers)
print(response.json())
You need to get an API key from SkyBiometry it sounds like. See section 1.1 below
https://skybiometry.com/documentation/
Every call to the API is required to be authorized, which basically means that you must sign up and obtain API access keys before accessing the service. Once that is done, you are ready to go coding.
SkyBiometry provides two ways for the client to authenticate himself:
*api_key and api_secret – by choosing this method every call to API must include api_key and api_secret. By using this fields we can map API call to the API user.
api_key and domain authentication – there are some situations when disclosing api_secret is not an option (like using java script client code or flash). Instead of disclosing api_secret, API user can choose domain authentication method. During the call there is no need to specify api_secret, instead user will be authenticated by calling domain, which will be compared to specified in user profile settings.
I have a python script running on a Raspberry Pi, and this script gathers real-time information about the Raspberry Pi, storing the information into variables. I would like to use an iPhone app that I am creating to retrieve this information and display it on screen each time a button is pressed.
I have found solutions online involving REST APIs as well as software like ZeroMQ; however, I am very unfamiliar with these types of things and am unsure which approach to use. What is the simplest approach that you would suggest, and how might I go about implementing it?
Here's a few ways I thought of doing it:
Use SMPT email protocol to send yourself an email which you can access on your iphone.
Pay for an SMS service to send SMS messages to your iphone.
Push to https://jsonstorage.net/ on your Rasberry Pi, then access the storage bin on your iphone.
If it's running Windows or Linux. You can download Dropbox Desktop. This will give you a folder that is automatically updated on changes which you can check from anywhere.
Further details:
How to update a json bin with python
headers = {'Content-Type': "application/json; charset=utf-8",'dataType': "json"}
data = json.dumps(json_data_you_want_to_upload)
url = https://jsonstorage.net/api/items/your-bin-id
resp = requests.put(url, headers=headers, data=data)
if resp.status_code == 200 or 201:
print(f'Data successfull uploaded: {resp.json()})')
else:
print(f'Error! Could not upload to the database: {resp.json()}')
I want my facebook chatbot to send multiple responses at the same time.
For example:
When the user says:
Hey
The chatbot should respond with:
Hey!
How are you?
Currently using dialogflow and a webhook built using python to build responses.
It is possible. But not in a way I consider clean.
You can add a response under the 'default' tab.
Under the 'facebook' tab you can than tick Use response from the DEFAULT tab as the first response.. This way two responses are send to facebook and to facebook only.
But you can send messages to the messenger via the facebook send api from your webhook at any time. Have a look at: https://developers.facebook.com/docs/messenger-platform/reference/send-api
This has nothing to do with dialogflow though.
You can find the id of the facebook user in the payload, dialogflow send to your webhook.
Here is an example of where you can find it.
{:originalDetectIntentRequest
{:payload
{:source "facebook",
:data
{:recipient {:id "1144092719067446"},
:sender {:id "1235572976569567"}, <== This is the id you are looking for!
:timestamp 1.536065422409E12,
:message
{:mid
"YzKAMwoJlAR0n3Vke2RJf83aVMGWzBnJ77SfUDe_NwkzIT1BBQXaWPVjN6Qf0xN4veairdW504PoKzcKV3lKBw",
:seq 183071.0,
:text "They are loyal"}}},
:source "facebook"},
...
...}
I've created a IBM Watson IoT platform, and a device type "testdevice".
I would like a simple test where I push data via a Python script and the HTTP API.
So far I've managed to be able to push data, but getting:
(403, 'Not allowed')
I can see in the IBM Watson IoT log that my computer has tried to push data, but Authentication failed.
As a request header I'm using auth=('use-token-auth', 'MY-TOKEN')
so the entire code is:
import requests
response = requests.post('http://MY-ORG-ID.messaging.internetofthings.ibmcloud.com:1883/api/v0002/device/types/testdevice/devices/MY-DEVICE/events/test', data={'number': 1}, auth=('use-token-auth', 'MY-TOKEN'), headers={'Content-type': 'application/json'})
print(response.text) #TEXT/HTML
print(response.status_code, response.reason) #HTTP
What could be the reason for authentication failing?
Have you tried connecting over HTTPS? Since the middle of the year, by default, organizations are configured to reject insecure connections, unless a user specifically enables this (you can find this option in the setting panel in the dashboard if you want to enable unencrypted connectivity in your organization), this is one possible reason you are getting 403 not allowed responses.
FYI, you might also be interested in the Python client library, which supports a HTTP only connection as well as the more feature rich MQTT client:
pip install ibmiotf
import ibmiotf.device
options = {"org": orgId, "type": "testdevice", "id": "MY-DEVICE", "auth-method": "token" , "auth-token": "MY-TOKEN"}
client = ibmiotf.device.HttpClient(options)
data={'number': 1}
client.publishEvent("test", "json", data)
Your code does work for me, so could be as DavidParker says http is not allowed. It could also be your token is getting messed up. I always encode the authentication header - x="use-token-auth:password".encode('base64')
`
Or, that token you have, is wrong.