Ich versuche mit den Cloud-Funktionen für Firebase einen Apples-Bestätigungs-Server anzurufen. Irgendeine Idee, wie man einen HTTP-Aufruf macht?
Denken Sie daran, dass Ihr Abhängigkeitsabdruck sich auf die Bereitstellungs- und Kaltstartzeiten auswirkt . So benutze ich https.get()
und functions.config()
, um andere, von Funktionen unterstützte Endpunkte zu pingen. Sie können denselben Ansatz auch beim Aufrufen von Drittanbieterdiensten verwenden.
const functions = require('firebase-functions');
const https = require('https');
const info = functions.config().info;
exports.cronHandler = functions.pubsub.topic('minutely-tick').onPublish((event) => {
return new Promise((resolve, reject) => {
const hostname = info.hostname;
const pathname = info.pathname;
let data = '';
const request = https.get(`https://${hostname}${pathname}`, (res) => {
res.on('data', (d) => {
data += d;
});
res.on('end', resolve);
});
request.on('error', reject);
});
});
Antwort wird aus der Bearbeitung von OP in Frage kopiert
Das OP wurde mit https://github.com/request/request behoben. _
var jsonObject = {
'receipt-data': receiptData,
password: functions.config().Apple.iappassword
};
var jsonData = JSON.stringify(jsonObject);
var firebaseRef = '/' + fbRefHelper.getUserPaymentInfo(currentUser);
let url = "https://sandbox.iTunes.Apple.com/verifyReceipt"; //or production
request.post({
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
url: url,
body: jsonData
}, function(error, response, body) {
if (error) {
} else {
var jsonResponse = JSON.parse(body);
if (jsonResponse.status === 0) {
console.log('Recipt Valid!');
} else {
console.log('Recipt Invalid!.');
}
if (jsonResponse.status === 0 && jsonResponse.environment !== 'Sandbox') {
console.log('Response is in Production!');
}
console.log('Done.');
}
});
meistens mit https://nodejs.org/api/https.html
const http = require("http");
const https = require('https');
const mHostname ='www.yourdomain.info';
const mPath = '/path/file.php?mode=markers';
const options = {
hostname: mHostname,
port: 80, // should be 443 if https
path: mPath ,
method: 'GET',
headers: {
'Content-Type': 'application/json'//; charset=utf-8',
}
};
var rData=""
const req0 = http.request(options, (res0)=>
{
res0.setEncoding('utf8');
res0.on('data',(d) =>{
rData+=d;
});
res0.on('end',function(){
console.log("got pack");
res.send("ok");
});
}).on('error', (e) => {
const err= "Got error:"+e.message;
res.send(err);
});
req0.write("body");//to start request