Verify domain of cloud function for webhook - firebase

How can I setup a webhook for the google calendar api?
I'm currently making a post to the api, stating the webhook/ address https://us-central1-pushmessage-bd1eb.cloudfunctions.net/getUsers
axios({
method: "POST",
url:
"https://www.googleapis.com/calendar/v3/calendars/xxx/events/watch",
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json;charset=UTF-8",
Authorization: token.token_type + " " + token.access_token,
"Content-Type": "application/json"
},
data: {
id: "tester",
type: "web_hook",
address: "https://xxx.cloudfunctions.net/getUsers"
}
})
However, I get the error:
code: 401,
message: 'Unauthorized WebHook callback channel: https://us-central1-xxx.cloudfunctions.net/getUsers' }
How can I verify the domain of my cloud function?
I have tried this method but still get the 401:
How to perform domain verification for Firebase functions

Related

Axios post request returning 403 when sending firebase notification

Hi I am trying to send notification to my app using firebase post method its working fine in postman but when I try to do post method in my app its returning 403. I tried implementing another get request its working fine so no problem with axios setup.
export const postData = function (url, payload) {
return AXIOS.post(url, payload, config);
};
let config = {
headers: {
'Content-Type': 'application/json',
"Authorization" : "key= *****myKey****"
}
};
Here is my post mehtod
let body = {
"to": "******myTokeb*******",
"notification": {
"title": "Title here",
"body": "R(body)",
"mutable_content": true,
"sound": "Tri-tone"
}
};
postData("http://fcm.googleapis.com/fcm/send",body).then((d)=>{
console.log("d",d)
}).catch((e)=>{
console.log("e",e);
});
Error
Error: Request failed with status code 403
at createError (D:\projects\fiver\Roeyat\node_modules\axios\lib\core\createError.js:16)
at settle (D:\projects\fiver\Roeyat\node_modules\axios\lib\core\settle.js:17)
at EventTarget.handleLoad (D:\projects\fiver\Roeyat\node_modules\axios\lib\adapters\xhr.js:61)
at EventTarget.dispatchEvent (D:\projects\fiver\Roeyat\node_modules\event-target-shim\dist\event-target-shim.js:818)
at EventTarget.setReadyState (D:\projects\fiver\Roeyat\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:567)
at EventTarget.__didCompleteResponse (D:\projects\fiver\Roeyat\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:389)
at D:\projects\fiver\Roeyat\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:502
at RCTDeviceEventEmitter.emit (D:\projects\fiver\Roeyat\node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189)
at MessageQueue.__callFunction (D:\projects\fiver\Roeyat\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:425)
at D:\projects\fiver\Roeyat\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112
Use HTTPS in postData
That will work!

Firebase Messaging Can't Send - How to get an OAuth Bearer Token using .Net or JavaScript?

I've been following the Firebase Messaging tutorial for a while trying to get notifications to work in a basic .Net Core app using JavaScript with ServiceWorker before I try to implement it in my main app. I'm failing at posting a message using jQuery's post with a response of 401 - "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential and a status of unauthenticated.
The bearer token is the reason because I'm using the same one as in the tutorial and this took me a while to realize because I cannot find any explanation of what this token is or that I needed my own, the tutorial seems to skip that part. After exhaustive browsing through Google docs I have found this, https://firebase.google.com/docs/cloud-messaging/auth-server?authuser=0 which is what the person who commented below has shared. It is exactly what is needed, problem is that it's done in Node. For those who don't use Node, can't do anything with it. Luckily right above Google states that we can use our preferred language using the Google API Client Library, except again I'm met with an exhaustive search. The same methods used in the Node example don't seem to be in the Google API Client Library and the single example shown for each language is how to get a token to use with a Google service like the People service and getting access to a users information. I don't see how that can be translated to getting an OAuth bearer token to authenticate with FCM so notifications can be sent. I've tried many combinations and I don't see any documentation showing what methods are available to find any similar methods that are being used in the Node example.
I just need a bearer token for below. Does anyone know how to get a bearer token for usage with FCM using either .Net or JavaScript like the Google doc says you can do?
$.post({
method: "POST",
url: "https://fcm.googleapis.com/v1/projects/floridarecycling-b91ec/messages:send",
dataType: "json",
contentType: "application/json",
headers: {
'Authorization': 'Bearer ' + 'ya29.ElqKBGN2Ri_Uz...HnS_uNreA'
},
data: {
"message": {
"token": "my device token",
"notification": {
"body": "This is an FCM notification message!",
"title": "FCM Message",
}
}
},
success: function () { console.log("Success") },
error: function (err) { console.log("error ", err) }
});
$.post({
method: "POST",
url: "https://fcm.googleapis.com/v1/projects/floridarecycling-b91ec/messages:send",
dataType: "json",
contentType: "application/json",
data: {
"message": {
"token": "my device id",
"notification": {
"body": "This is an FCM notification message!",
"title": "FCM Message",
}
}
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA')
},
success: function () { console.log("Success") },
error: function (err) { console.log("error ", err) }
});

Token based auth for aspnet-core web api

Dears,
I've followed https://stormpath.com/blog/token-authentication-asp-net-core
to authenticate the user for my web apis
I managed to create a successful access-token when calling api/token
My problem is the use of [Authorize], authorize filter didn't get that my user has a valid token, although HeaderAuthorization and HeaderExpries have been set.
function getValues()
{
$.ajax({
url: "http://localhost:48146/api/values",
headers: { 'Authorization': 'Basic ' + accessToken, Expires: tokenExpires },
method: "GET",
context: document.body,
success: function (data) {
alert(data);
}
});
}
Did I passed a wrong header?
Based on the tutorial you followed you should pass a bearer authorization header, not a basic authorization header:
headers: { 'Authorization': 'bearer' + accessToken, Expires: tokenExpires },
I've figured out 2 problems
as #user1336 said in header I had theaders: { 'Authorization': 'bearer' + accessToken, Expires: tokenExpires },
I had to call ConfigureAuth(app) before app.UseMvc(); in Startup.css

Instagram API Subscription For First Time ( get real time update )

I have registered my APP on Instagram and i Successfully retrieved recent media using:
https://api.instagram.com/v1/users/self/media/recent/?access_token=ACCESS-TOKEN
Now I want to Subscribe user to instgram to get real time update , i tried this :
$.ajax({
url: "https://api.instagram.com/v1/subscriptions/?client_id=MyClientID&client_secret=MyClientSecret&object=user&aspect=media&verify_token=MyToken&callback_url=MyService.ashx",
type: 'POST',
dataType: "jsonp",
success: function (data) {
console.log(data);
},
error: function (errorText) {
console.log(errorText);
}
});
});
the Post request above prints : {"meta": {"code": 200}, "data": []}
which means it is a successful request
but i didn't receive any request from Instagram on my callback URL to send me the [ hub.challenge ]
Instagram subscription API :
Create a Subscription
To create a subscription, you make a POST request to the subscriptions endpoint.
Where I went Wrong ??

Meteor PayPal Payments (using Meteor.http)

Edit: I've fixed my original problem and have shown a metor example in my answer.
I'm getting a error 500 when trying to get the token for my PayPal API app in Meteor:
token = EJSON.stringify(Meteor.http.call "POST", "https://api.sandbox.paypal.com/v1/oauth2/token",
headers:
"Accept": "application/json"
"Accept-Language": "en_US"
auth: "user:pass"
params:
"grant_type":"client_credentials"
);
console.log("Token: "+token);
Output of this code:
Token: {"statusCode":500,"headers":{"server":"Apache-Coyote/1.1","date":"Fri, 15 Mar 2013 05:04:43 GMT","content-length":"0","connection":"close"},"data":null,"error":{}}
Obviously PayPal is returning a error 500 to me. I can't figure out what may be causing this. Of course Auth is actual data, not user:pass.
Why am I getting error 500?
Edit: Compiled Javascript
var token;
token = EJSON.stringify(Meteor.http.call("POST", "https://api.sandbox.paypal.com/v1/oauth2/token", {
headers: {
"Accept": "application/json",
"Accept-Language": "en_US"
},
auth: "user:pass",
params: {
"grant_type": "client_credentials"
}
}));
console.log("Token: " + token);
Here's an example implementation to make paypal API calls with meteor.
In the startup of your program, fetch your token. Always replace clientid and clientsecret with your own.
token = EJSON.parse(Meteor.http.post("https://api.sandbox.paypal.com/v1/oauth2/token",
headers:
"Accept": "application/json"
"Accept-Language":"en_US"
auth: "clientid:clientsecret"
params:
"grant_type":"client_credentials"
#encoding: "base64"
).content).access_token;
Now, create a payment, shown here in a Meteor.methods method (and returning a URL for the client to go to):
buySingleItem: () ->
console.log "Starting new payment, user id: "+Meteor.userId()
result = Meteor.http.post("https://api.sandbox.paypal.com/v1/payments/payment",
headers:
"Authorization":"Bearer "+token
"Content-Type": "application/json"
data:
{
"intent":"sale"
"redirect_urls":
"return_url":"http://mysite.herokuapp.com/done",
"cancel_url":"http://mysite.herokuapp.com/cancel"
"payer":
"payment_method":"paypal"
"transactions":[
{
"amount":
"total":"3.00",
"currency":"USD"
"description":"My item description."
}
]
}
)
payment = result.data
console.log "PayPal redirect: "+payment.links[1].href
return payment.links[1].href
This will create a PayPal checkout style payment, within Meteor.
I would provide sample code, but I'm not familiar with Meteor.
Basically you have 2 issues here:
in your headers, you are not passing the client id or client secret. This should look like:
Authorization: Basic clientid:clientsecret
Also, in your request, your request should look like this:
response_type=token&grant_type=client_credentials
Looks like your in json then stringifying it, so whatever way you need to get the POST request I just put up there, once you get it, you should be good.
[edit]PayPal's doc's dont have you base64 encode the client id or secret[/edit]
Then, when you need to execute the payment you can do as below. See the whole payment process here.
Meteor.methods
'executePaypalPayment': (payerId) ->
payment = PaypalPayments.findOne({ userId: #userId },
{ sort: { 'create_time': -1 } })
token = Meteor.call 'getPaypalToken'
url = 'https://api.sandbox.paypal.com/v1/payments/payment/' +
payment.id + '/execute'
res = Meteor.http.post url,
headers:
Authorization: 'Bearer ' + token.access_token
'Content-Type': 'application/json'
data:
payer_id: payerId
payment = res.data
payment['userId'] = #userId
if payment.state is 'approved'
# we insert the sucessful payment here
PaypalPayments.insert payment
return if payment.state is 'approved' then true else false

Resources